Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How do I access menus in resource dictionaries?

During runtime the main menu should only show the menus associated to the current view. Also I want to use some of the menus as context menus inside the corresponding views. I do not want to write the same menues two or more times, so I thought about putting them into resource dictionaries to reuse them if needed.

I want a main application menu like this:

XML
<StackPanel DockPanel.Dock="Top" >
    <Menu>

        <MenuItem Header="_Proxy">
            <MenuItem Header="_Connect" Command="{Binding Path=ProxyConnection.Connect}" IsChecked="{Binding Path=ProxyConnection.IsConnected}" />
            <MenuItem Header="_Disconnect" Command="{Binding Path=ProxyConnection.Disconnect}" />
        </MenuItem>

        <MenuItem Header="_foo">
            <MenuItem Header="_bar" Command="{Binding Path=Foo.Bar}" />
            <MenuItem Header="_vodoo" Command="{Binding Path=Foo.Vodoo}" />
        </MenuItem>

    </Menu>
    ...


I put the menus in a resorce dicitonary Menus.xaml like

XML
<MenuItem Header="_Proxy" x:Key="proxyMenu" >
            <MenuItem Header="_Connect" Command="{Binding Path=ProxyConnection.Connect}" IsChecked="{Binding Path=ProxyConnection.IsConnected}" />
            <MenuItem Header="_Disconnect" Command="{Binding Path=ProxyConnection.Disconnect}" />
</MenuItem>


<MenuItem Header="_foo" x:Key="fooMenu" >
    <MenuItem Header="_bar" Command="{Binding Path=Foo.Bar}" />
    <MenuItem Header="_vodoo" Command="{Binding Path=Foo.Vodoo}" />
</MenuItem>

The I use resource directories like this, or am I not?
XML
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Menus.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

And then?
XML
<StackPanel DockPanel.Dock="Top" >
    <Menu>
        <!-- following throws an exception -->
        <MenuItem ItemsSource="{StaticResource proxyMenu}" />

I tried different bindings but nothing worked as I wanted. the menus did not appear.


Thanks for any hints.
Andy
Posted
Comments
Jeff Blankenburg 1-Mar-13 8:41am    
My initial thought, at first glance, is that you should be binding to the <menu> and not the <menuitem>. Have you tried that?
Andy411 1-Mar-13 9:50am    
Thx for your comment.
But sorry, I don't understand. It looks like something is missing in your text?
Jeff Blankenburg 1-Mar-13 9:53am    
Updated. Sorry.
Andy411 4-Mar-13 4:53am    
I tried <menu ItemsSource="{StaticResource proxyMenu}">. I get an exception ""System.Windows.Controls.MenuItem Header:_Proxy Items.Count:2" is not a valid value for property ItemsSource"

1 solution

Hi,

the ItemsSource property of the Menu or MenuItem expects an collection of other items.
So your binding will not work.

You could define in the Menus.xaml a CompositeCollection of MenuItem's and bind these to Menu or MenuItem like so:

XML
<CompositeCollection x:key="MenuTest">
    <MenuItem Header="_Proxy">
        <MenuItem Header="_Connect" />
        <MenuItem Header="_Disconnect" />
    </MenuItem>
</CompositeCollection>


And in your Window:
XML
<Menu ItemsSource="{StaticResource MenuTest}" />

or
XML
<Menu>
  <MenuItem Header="TestMenu" ItemsSource="{StaticResource MenuTest}" />
</Menu>


Hope this helps,

Thomas.
 
Share this answer
 
v3
Comments
Andy411 4-Mar-13 11:07am    
Thx Thomas,
it works and helped me a lot and I could walk a big step forward.
cu
Andy
Thomas Duwe 4-Mar-13 11:30am    
Glad I could help :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900