This project has moved. For the latest updates, please go here.

Menus and Toolbars

 Menu's and toolbars can be merged together from xaml in the main applications window (or other window) and from a module (or same app if you desire).  (This functionality was taken from a great open source example on CodeProject, and slightly modified).

 

Example of Menu resources in a Shell.xaml file:

 

<Window.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>


                    <MenuItem Header="File"  x:Key="fileMenu" mm:MergeMenus.Id="mergefileMenu" mm:MergeMenus.Priority="10" mm:MergeMenus.HostId="MainMenu">
                        <MenuItem Header="Open" Command="ApplicationCommands.Open" mm:MergeMenus.Priority="10"/>
                        <MenuItem Header="Save" Command="ApplicationCommands.Save" mm:MergeMenus.Priority="20"/>
                        <MenuItem Header="Save As..." Command="ApplicationCommands.SaveAs" mm:MergeMenus.Priority="30"/>
                        <MenuItem Header="Exit" Command="w:AppCanCommands.Exit" mm:MergeMenus.Priority="100000"/>
                    </MenuItem>
                    <MenuItem Header="Edit"  x:Key="editMenu" mm:MergeMenus.Id="mergeeditMenu" mm:MergeMenus.HostId="MainMenu" mm:MergeMenus.Priority="20">

                        <MenuItem Header="Copy" Command="ApplicationCommands.Copy" mm:MergeMenus.Priority="10"/>
                        <MenuItem Header="Paste" Command="ApplicationCommands.Paste" mm:MergeMenus.Priority="20"/>

                    </MenuItem>



                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
Example of a menu that will be merged from a module's view to the shell's menu:
<UserControl x:Class="HelloWorldModule.Views.MenuView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xmlns:w="clr-namespace:AppCan.wpf.Application;assembly=AppCan.wpf"
             xmlns:mm="clr-namespace:AppCan.wpf.Menus;assembly=AppCan.wpf"
             d:DesignHeight="300" d:DesignWidth="300">
    
    
    
    
    <Grid Name="grid_main">
        <Menu DockPanel.Dock="Top" x:Name="mainMenu" mm:MergeMenus.Id="MainMenu">

            <MenuItem Header="_Help" x:Name="HelpMenuItem" mm:MergeMenus.Id="HelpMenu" mm:MergeMenus.Priority="100000"/>
        </Menu>
    </Grid>
</UserControl>

 
 
Note the mm:MergeMenus.Id is the name of the MergeMenu.  In this case "MainMenu".
 
Notice the Shell's menu resource has in it's menu mm:MergeMenus.HostId="MainMenu" which indicates
to merge the current menu into the host menu.  In the case of the Shell's resources that is the "MainMenu".
So the Module's menu is actually the main menu that will be merged into. (Probably should do this in the reverse way,  but this is just an example. 
You will also see each menu item has a priority on it (i.e mm:MergeMenus.Priority="30") .  These should be from lower numbers to higher by default in your xaml or sorting by value won't work properly.  Lower numbers get inserted before higher numbers.
You can also do this from code by manually setting the dependency properties on Menu's for the MergeMenu ID, Host ID and Priority.
 
You can find this example in the SimpleApp sample for more details.  Also the API docs in the docs directory have some additional details.
This functionality came from a great code project post here.  It's been slightly modified in AppCan.

Last edited Mar 31, 2013 at 3:19 PM by davesspacebar, version 5

Comments

No comments yet.