I have Menu in my app. I'm visualizing it using hierarchical data template:
<MenuItem Header="Main menu" ItemsSource="{Binding ApplicationMenu}" >
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type tm:RMenuItem}"
ItemsSource="{Binding Path=ChildrenItems}">
<MenuItem Header="{Binding Name}" Command="{Binding RunOperationCommand}" />
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
menu looks like as it should, but Command for each menu item is not fired! Even more - it is not bounded, which could be seen in debugger: get accessor of ICommand Property has been never executed. Why it happens so?
Doing as usual works perfect:
<Menu>
<MenuItem Header="SomeHeader" Command="{Binding RunOperationCommand}"/>
<Menu>
The difference between the first and the second example in your question is that in the second code snippet you are binding MenuItem.Command
to the parent's data context, which has the RunOperationCommand
defined. Whereas in the first example with the HierarchicalDataTemplate
you are binding to the "local" DataContext, which is a menu item. It doesn't have the appropriate property, so the binding fails.
You have several options:
<MenuItem Header="Main menu" ItemsSource="{Binding ApplicationMenu}" >
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type tm:RMenuItem}"
ItemsSource="{Binding Path=ChildrenItems}">
<MenuItem Header="{Binding Name}"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.RunOperationCommand}"
/>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
<Window.Resources>
<coreView:CommandReference x:Key="RunOperationCommand"
Command="{Binding RunOperationCommand}" />
</Window.Resources>
<MenuItem Header="Main menu" ItemsSource="{Binding ApplicationMenu}" >
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type tm:RMenuItem}"
ItemsSource="{Binding Path=ChildrenItems}">
<MenuItem Header="{Binding Name}"
Command="{StaticResource RunOperationCommand}"
/>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>