You should see this in the Visual Studio output window:
Error System.Windows.Data: 4: Unable to find source for binding with reference 'ElementName = I.' BindingExpression: Path = MenuText; DataItem = NULL; target element 'MenuItem' (Name = ''); target property "Heading" (type "Object")
This is because ContextMenu is disconnected from VisualTree, you need to do this Binding differently.
One way is through ContextMenu.PlacementTarget (which should be a grid), you can use your DataContext to set the binding, for example:
<MenuItem Header="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.MenuText}"/>
or configure the DataContext in the ContextMenu itself:
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.DataContext}"> <MenuItem Header="{Binding Path=MenuText}"/> </ContextMenu>
If this is not an option (since the DataContext Grid cannot be a Window / UserControl), you can try passing a link to the Window / UserControl via the Tag your grid, for example.
<Grid ... Tag="{x:Reference self}"> <Grid.ContextMenu> <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.Tag}"> <MenuItem Header="{Binding Path=MenuText}"/> </ContextMenu> ...
As a side note: because of this behavior, I tend to define a helper style in App.xaml to make all ContextMenus βinheritβ the DataContext from their parent:
<Style TargetType="{x:Type ContextMenu}"> <Setter Property="DataContext" Value="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"/> </Style>
source share