WPF Submenu for context menu
You can set MenuItem.ItemsSource
to the nested collection. This will generate the submenu for any menuitem. For this the model backing your MenuItem should have submenuitems collection in it
<ContextMenu ItemsSource="{Binding ContextMenuItemsSource}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="ItemsSource" Value="{Binding ContextMenuSubItems}"></Setter>
<Setter Property="Header" Value="{Binding ContextMenuCommandHeader}"></Setter>
<Setter Property="Command" Value="{Binding ContextMenuCommand}"></Setter>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
Similarly you can set the MenuItem.ItemContainerStyle
and MenuItem.ItemTemplate
to style your submenuitems.
Specifying explicitly, without binding a collection you can add sub menus by nesting them.
A MenuItem can have other MenuItem elements within it as child/sub menus and can go up to several levels. The following code adds three children menu items to first menu item.
<MenuItem Header="_File">
<MenuItem Header="_Open" IsCheckable="true" />
<MenuItem Header="_Close" IsCheckable="true" />
<MenuItem Header="_Save" IsCheckable="true" />
</MenuItem>
Source: https://www.c-sharpcorner.com/uploadfile/mahesh/menus-in-wpf/