Multiple usage of MenuItems declared once (WPF)

Posted by Alex Kofman on Stack Overflow See other posts from Stack Overflow or by Alex Kofman
Published on 2010-06-16T10:32:20Z Indexed on 2010/06/16 10:52 UTC
Read the original article Hit count: 241

Filed under:
|
|
|
|

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}" />

© Stack Overflow or respective owner

Related posts about .NET

Related posts about wpf