wpf System.Windows.Data 错误:4:无法找到引用绑定的源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18768390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:38:30  来源:igfitidea点击:

System.Windows.Data Error: 4 : Cannot find source for binding with reference

c#wpf

提问by

I have 3 Projects in a single VS 2012 Solution. Below is the XAML file that is throwing this error is in Project A . The BmpImage.csfile exists in different project B in the same solution which contains all the extensions and helper files and contains all methods and code that is required . Project A references Project B.

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

Any Help please my job is on the line here . I am stuck. I have read all other similar questions but none does address this issue

任何帮助请我的工作就在这里。我被困住了。我已经阅读了所有其他类似的问题,但没有一个解决这个问题

I am getting this error :

我收到此错误:

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

System.Windows.Data 错误:4:无法找到引用“RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Button', AncestorLevel='1''的绑定源。BindingExpression:Path=IsEnabled; 数据项=空;目标元素是 'BmpImage' (Name=''); 目标属性是“NoTarget”(类型“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>

回答by Sheridan

Your code looks about right... have you tried the Bindingexpression without the optional AncestorLevel=1? I generally use this kind of Binding, but I never bother with the AncestorLevelproperty because it couldcause an error if it is set wrong, or if the XAML was ever changed.

你的代码看起来是对的……你试过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>

回答by Amol Bavannavar

Use following Code :

使用以下代码:

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

Write the XAML Code as :

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