I`m wondering how can I bind MenuItem.Header to the parent Window/UserControl dependency property? Here is a simple example:
Window1.xaml:
You should see this in the Output window of Visual Studio:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=self'. BindingExpression:Path=MenuText; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Header' (type 'Object')
That is because the ContextMenu is disconnected from the VisualTree, you need to do this Binding differently.
One way is via ContextMenu.PlacementTarget
(which should be the Grid), you could use its DataContext to establish a binding, e.g.:
or set up the DataContext in the ContextMenu itself:
If this is not an option (because the DataContext of the Grid cannot be the Window/UserControl) you can try to pass the reference to the Window/UserControl through the Tag
of your Grid for example.
...
As a side-note: Because of this behavior i tend to define a helper-style in App.xaml
to make all ContextMenus "pseudo-inherit" the DataContext from their parent: