你有没有遇到过这样的问题:
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')
代码:
<ContextMenu DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
上下文菜单位于:
ListBox-> ListBoxItemTemplate -> DataGrid -> DataGrid.ContextMenu
我在 ListBox 控件中还有另一个 Binding,它完全没有问题。