c# - 系统.Windows.数据错误 : 4 : Cannot find source for binding with reference

标签 c# wpf

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

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

我收到这个错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Button', AncestorLevel='1''. BindingExpression:Path=IsEnabled; DataItem=null; target element is 'BmpImage' (Name=''); target property is 'NoTarget' (type 'Object')

<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>

最佳答案

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

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type 
    Button}}, Path=IsEnabled}" Value="False">
    <Setter Property="UIElement.Opacity" Value="0.3" />
</DataTrigger>

关于c# - 系统.Windows.数据错误 : 4 : Cannot find source for binding with reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18768390/

相关文章:

C# ArgumentOutOfRangeException 参数智能感知

c# - 为什么我不能序列化具有私有(private) setter 的属性(C#、json)

c# - 意外的相等测试,Equals(a, a) 评估为 false

c# - 检查整数类型是否属于枚举成员

c# - 当 Controller Action 同步时,有什么理由使用异步/等待吗?

c# - 将 stackPanel 的最后一项水平附加到右侧

.net - 如何从 WPF 中的另一个线程读取 textbox.Text 值?

c# - 以通用方式从 root 获取 XAML 中的子元素属性

wpf - 开始 WPF 测试的最简单和最便宜的方法

c# - 在哪里初始化专门的 wpf 控件?