WPF相对源 - 无法为参考结合查找源 [英] WPF Relative source- Cannot find source for binding with reference

查看:324
本文介绍了WPF相对源 - 无法为参考结合查找源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你曾经有一个这样的问题:

have you ever have a problem like this:

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

code:

<ContextMenu DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" 

右键菜单中的:

ListBox-> ListBoxItemTemplate -> DataGrid -> DataGrid.ContextMenu

我也陆续在ListBox控件,在所有没有问题的作品装订。

I have also another Binding in ListBox control, that works with no problems at all.

推荐答案

解决方案:

1)的ContextMenu所有者设置标签属性的DataContext你想要的。
2)文本菜单集的DataContext到

1) Set Tag property on ContextMenu owner to DataContext you wanted. 2) Set DataContext of ContextMenu to

DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}} 

3)绑定的内容是这样的:

3) Bind your elements like this:

Name_Of_Property="{Binding Tag.Name_Of_Property}"

这篇关于WPF相对源 - 无法为参考结合查找源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆