数据模板绑定垃圾邮件输出窗口出现错误:找不到管理 FrameworkElemen

2024-05-15

我有问题

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。 BindingExpression:(无路径);数据项=空;目标元素是“SolidColorBrush”(HashCode=48519443);目标属性是“颜色”(类型“颜色”)

可以使用以下 xaml 重现它:

<Window PreviewKeyDown="Window_PreviewKeyDown"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
        ... >
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:ViewModel}">
            <ListBox ItemsSource="{Binding List}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border Width="20" Height="20">
                            <Border.Background>
                                <SolidColorBrush Color="{Binding diag:PresentationTraceSources.TraceLevel=High}" />
                            </Border.Background>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DataTemplate>
    </Window.Resources>
    <ContentControl Content="{Binding}" />
</Window>

and cs:

public class ViewModel
{
    public List<Color> List { get; } = new List<Color> { Colors.Red, Colors.Blue, Colors.Green, Colors.Black, Colors.Yellow };
}

public partial class MainWindow : Window
{
    readonly ViewModel _vm = new ViewModel();

    public MainWindow()
    {
        InitializeComponent();
    }

    void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.F1)
            DataContext = _vm;
        if (e.Key == Key.F2)
            DataContext = null;
    }
}

Start, press F1 and check Output window (binding tracing is enabled, so you will see more).

问题:我该如何修复它们?

错误不会导致功能问题,但它们会出现在Output窗口(在实际项目中有很多),这已经是一些问题的迹象。哪个问题?

这里的关键是拥有数据模板并应用它一段时间之后,那么datatemplate中的这个绑定就会出现问题。环境DataContext在构造函数中不会引起问题。


这是一些痕迹,如果有人能够理解它并可以给出一些提示:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=45377043) for Binding (hash=28932383)
System.Windows.Data Warning: 58 :   Path: ''
System.Windows.Data Warning: 60 : BindingExpression (hash=45377043): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=45377043): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=45377043): Attach to System.Windows.Media.SolidColorBrush.Color (hash=48519443)
System.Windows.Data Warning: 64 : BindingExpression (hash=45377043): Use Framework mentor <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=45377043): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=45377043): Framework mentor not found
System.Windows.Data Warning: 65 : BindingExpression (hash=45377043): Resolve source deferred
...
System.Windows.Data Warning: 67 : BindingExpression (hash=45377043): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=45377043): Framework mentor not found
System.Windows.Data Warning: 67 : BindingExpression (hash=45377043): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=45377043): Framework mentor not found
System.Windows.Data Warning: 67 : BindingExpression (hash=45377043): Resolving source  (last chance)
System.Windows.Data Warning: 69 : BindingExpression (hash=45377043): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:(no path); DataItem=null; target element is 'SolidColorBrush' (HashCode=48519443); target property is 'Color' (type 'Color')

我明白当数据模板创建元素时,mentor由于未找到与源的绑定(导师是什么?),因此绑定创建被推迟。然后它重复几次并因给定错误而失败。但最后它起作用了,也许它继续重复而不追踪它?有任何想法吗?


不幸的是,我无法向您提供有关绑定错误发生的原因以及绑定最终起作用的确切细节,但也许我能够对此进行一些说明。

这似乎正在发生:

因为您使用的是绑定而不指定源,所以默认情况下框架会尝试使用FrameworkElement.DataContext or FrameworkContentElement.DataContext目标上的属性作为绑定的源。这显然失败了,因为SolidColorBrush两者都不是派生的。而且,由于SolidColorBrush不是视觉树或逻辑树的一部分,它不能绑定回Border,因此框架无法解析绑定的源。但不确定在某些时候源代码是否已解析并且绑定是否正常工作是如何发生的。

然而,这个问题有一个解决方案,它是专门为了解决这种情况而引入的,它使用视觉或逻辑树之外的其他方式来解决绑定源。您需要做的就是定义您的SolidColorBrush作为资源:

<Border Width="20" Height="20">
    <Border.Resources>
        <SolidColorBrush x:Key="BackgroundBrush" Color="{Binding}" />
    </Border.Background>
    <Border.Background>
        <StaticResource ResourceKey="BackgroundBrush" />
    </Border.Background>
</Border>

通过这样做,您会在跟踪中得到一个主要差异,这似乎是无法获得结果的关键Error: 2:

...
System.Windows.Data Warning: 65 : BindingExpression (hash=38517915): Resolve source deferred
System.Windows.Data Warning: 95 : BindingExpression (hash=38517915): Got InheritanceContextChanged event from SolidColorBrush (hash=55584612)
...

该解决方案不仅适用于SolidColorBrush,但对于派生自的任何类型的对象Freezable(画笔、变换等)

但你必须小心,因为这很重要您将其放置在哪个资源字典中Freezable。如果你要把它放在Window.Resources字典源将解析为Window.DataContext (a ViewModel以您的情况为例)。

我的最终假设是,如果您使用这种技术,您的绑定设置Freezable已解决as if它们是在资源字典所有者上设置的(顺便说一下,我认为这被称为mentor or 治理元素)。这也适用于使用的绑定RelativeSource or ElementName.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数据模板绑定垃圾邮件输出窗口出现错误:找不到管理 FrameworkElemen 的相关文章

随机推荐