I am having trouble write the xaml representation to allow to bind to my background ViewModel for cascading menus
here’s the VM:
public class MenuNode
{
public string Header {get;}
public List<MenuNode> Items {get;}
}
the xaml i have is this:
<ContextMenu ItemsSource="{Binding Choices}" >
<ContextMenu.Resources>
<DataTemplate DataType="{x:Type vmi:MenuNode}">
<MenuItem Header="{Binding Header}" ItemsSource="{Binding Items}"/>
</DataTemplate>
</ContextMenu.Resources>
</ContextMenu>
When the menu pops up I get the first-level entries with an arrow(indicating that there should be a sub-menu) but when i hover over the menu it doesn’t show the sub-menu items.
Any ideas?
OK, here’s the issue:
For some reason, the
MenuItemsthat were generated by yourDataTemplateare getting wrapped inside of anotherMenuItem(the result was nestedMenuItems). The sub items were not being opened because the outerMenuItemhad no children.The solution is to use a
HierarchicalDataTemplateinstead: