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}}}"
Context Menu is in the:
ListBox-> ListBoxItemTemplate -> DataGrid -> DataGrid.ContextMenu
I have also another Binding in ListBox control, that works with no problems at all.
The solution:
1) Set Tag property on ContextMenu owner to DataContext you wanted. 2) Set DataContext of ContextMenu to
3) Bind your elements like this:
context menu is not part of the visual tree, so you use PlacementTarget within your binding
this would use the DataContext of the DataGrid