4

我在一个 VS 2012 解决方案中有 3 个项目。以下是引发此错误的 XAML 文件位于项目 A 中。该BmpImage.cs文件存在于同一解决方案中的不同项目 B 中,其中包含所有扩展和帮助文件,并包含所需的所有方法和代码。项目 A 引用项目 B。

有人可以帮忙吗?我的工作就在这里,但我被困住了。我已经阅读了所有其他类似的问题,但没有一个能解决这个问题。

我收到此错误:

System.Windows.Data 错误:4:找不到与引用'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.Button',AncestorLevel='1''的绑定源。绑定表达式:路径=IsEnabled;数据项=空;目标元素是'BmpImage'(名称='');目标属性是“NoTarget”(类型“对象”)

<UserControl x:Class="MyGrid.MyPanel" 
         Name="_ctrl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ex="clr-namespace:Helper.Xaml;assembly=Helper.Xaml" 
         xmlns:sx="clr-namespace:MyPanel.Xaml;assembly=MyPanel.Xaml">
<UserControl.Resources>
    **<Style x:Key="BmpStyle" TargetType="{x:Type ex:BmpImage}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type  Button}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
              <Setter Property="UIElement.Opacity" Value="0.3" />
            </DataTrigger>
         </Style.Triggers>
    </Style>**
</UserControl.Resources>
<Grid>
    <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <ToolBar ToolBarTray.IsLocked="True">
         <Button ToolTip="Filter..." ToolTipService.ShowOnDisabled="True" Click="Filter_OnClick">
             <ex:BmpImage Source="Images/filter.png" Style="{StaticResource BmpStyle}" />
         </Button>
         <ToggleButton ToolTip="AutoScroll" ToolTipService.ShowOnDisabled="True" IsChecked="{Binding ElementName=Trades, Path=AutoScroll}">
            <ex:BmpImage Source="Images/Autoscroll.png" Style="{StaticResource BmpStyle}" />
         </ToggleButton>
    </ToolBar>
    <sx:PanelGrid Name="PanelGrid" Grid.Row="1" />
</Grid>
</UserControl>
4

2 回答 2

1

使用以下代码:

using DevExpress.Xpf.Core.Native;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace BindingErrorHelper
{
    public class IsTypeFoundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            FrameworkElement element = value as FrameworkElement;
            Type type = parameter as Type;
            if (element != null && type != null)
            {
                element = LayoutHelper.FindElement(element, p => p.GetType() == type);
                if (element != null)
                    return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class LayoutHelper
    {
        public static FrameworkElement FindElement(FrameworkElement treeRoot, System.Predicate<FrameworkElement> predicate)
        {
            VisualTreeEnumerator visualTreeEnumerator = new VisualTreeEnumerator(treeRoot);
            while (visualTreeEnumerator.MoveNext())
            {
                FrameworkElement frameworkElement = visualTreeEnumerator.Current as FrameworkElement;
                if (frameworkElement != null && predicate(frameworkElement))
                {
                    return frameworkElement;
                }
            }
            return null;
        }
    }
}

将 XAML 代码编写为:

<tt:IsTypeFoundConverter x:Key="isTypeFoundConverter"/>

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource isTypeFoundConverter}, ConverterParameter={x:Type Button}}" Value="true"/>
        <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=IsEnabled}" Value="False"/>
    </MultiDataTrigger.Conditions>
    <MultiDataTrigger.Setters>
        <Setter Property="UIElement.Opacity" Value="0.3" />
    </MultiDataTrigger.Setters>
</MultiDataTrigger> 
于 2014-11-13T13:17:39.917 回答
1

您的代码看起来正确...您是否尝试过Binding没有可选的表达式AncestorLevel=1?我通常使用这种Binding,但我从不关心该AncestorLevel属性,因为如果设置错误,或者 XAML 曾经更改过,它可能会导致错误。

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type 
    Button}}, Path=IsEnabled}" Value="False">
    <Setter Property="UIElement.Opacity" Value="0.3" />
</DataTrigger>
于 2013-09-12T15:42:37.950 回答