Multiple usage of MenuItems declared once (WPF)
- by Alex Kofman
Is it possible in WPF to define some menu structure and than use it in multiple contexts? For example I'd like to use a set of menu items from resources in ContextMenu, Window's menu and ToolBar (ToolBar with icons only, without headers). So items order, commands, icons, separators must be defined just once.
I look for something like this:
Declaration in resources:
<MenuItem Command="MyCommands.CloneObject"
CommandParameter="{Binding SelectedObject}" Header="Clone">
<MenuItem.Icon>
<Image Source="Images\Clone.png" Height="16" Width="16"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="MyCommands.RemoveCommand"
CommandParameter="{Binding SelectedObject}" Header="Remove">
<MenuItem.Icon>
<Image Source="Images\Remove.png" Height="16" Width="16"></Image>
</MenuItem.Icon>
</MenuItem>
<Separator/>
<MenuItem Command="MCommands.CreateChild"
CommandParameter="{Binding SelectedObject}" Header="Create child">
<MenuItem.Icon>
<Image Source="Images\Child.png" Height="16" Width="16"></Image>
</MenuItem.Icon>
</MenuItem>
Usage:
<ToolBar MenuItems(?)="{Reference to set of items}" ShowText(?)="false" />
and
<ContextMenu MenuItems(?)="{Reference to set of items}" />