mabster

Hi gang,

In my application, I'd like to have a submenu (eg. under "File|New") with a series of items in it, each representing things the user can create.

I'd also like to have that menu available as the submenu of a "New" menuitem in a context menu.

And as a third option, I'd like to have a menu on a toolbar (disguised as a button) so that the user clicks this "New" button and the things he can create drop down as a menu.

Is there some way I can declare a menu structure once, and reuse it in multiple places throughout the code I tried something like this:

Code Snippet

<MENUITEM Header="_New" x:Key="NewMenu">

<MENUITEM Header="..." />

<MENUITEM Header="..." />

<MENUITEM Header="..." />

<MENUITEM Header="..." />

</MENUITEM>

... and then placed this code in a few places throughout my XAML:

Code Snippet

<StaticResource ResourceKey="NewMenu"/>

... but that didn't work. The app crashed when it ran. Presumably because it's trying to use the same instance of "NewMenu" in multiple places, rather than creating a new instance each time.

Any ideas how I can make this work Is it possible using styles or templates

Cheers,

Matt



Re: Windows Presentation Foundation (WPF) Reuse a Submenu?

Douglas Stockwell

You can specify to create a new instance for each reference by setting x:Shared="False" on the resource.





Re: Windows Presentation Foundation (WPF) Reuse a Submenu?

mabster

Awesome! Thanks for the fast and helpful reply, Doug!



Re: Windows Presentation Foundation (WPF) Reuse a Submenu?

mabster

As a follow-up to this:

How can I reuse just the list of submenu items, rather than the top-level "New" item and all its children

ie. If I had a list of menu items representing all the things I can create, but I want to reuse that list as the children of several pre-existing menu items.

I imagine the code to use the resource would be something like:

<MenuItem Header="_New">

<StaticResource ResourceKey="newMenuItems" />

</MenuItem>

... but what type, exactly, is the "newMenuItems" resource How do I define it

Thanks,

Matt





Re: Windows Presentation Foundation (WPF) Reuse a Submenu?

Douglas Stockwell

MenuItem is an ItemsControl so you can set an IEnumerable type on the ItemsSource.

<x:Array Type="MenuItem" Key="newMenuItems" x:Shared="False">

<MenuItem Header="A" />

<MenuItem Header="B" />

</x:Array>

...

<MenuItem Header="_New" ItemsSource="{StaticResource newMenuItems}">





Re: Windows Presentation Foundation (WPF) Reuse a Submenu?

mabster

Hi Doug,

Thanks again for the fast reply, but I can't get this to work. I've added this to the DockPanel.Resources section of the DockPanel in which the menus will live:

Code Snippet

<x:Array Type="MenuItem" Key="newMenuItems" x:Shared="False">

<MenuItem Command="{x:Static local:MainWindow.NewFolder}">

<MenuItem.Icon>

<Image Height="16" Source="Resources/Images/Icons/folder_open_16.png" />

</MenuItem.Icon>

</MenuItem>

</x:Array>

The code compiles, but even without ever referencing "newMenuItems" elsewhere in the XAML, the application crashes when I run it with a XamlParseException.

If I remove the above XAML, the app runs fine.

What have I missed

Cheers,

Matt





Re: Windows Presentation Foundation (WPF) Reuse a Submenu?

Douglas Stockwell

Try digging into the Inner Exception to see the real problem. I tried it out in a new app and the exception was "Shared attribute in namespace 'http://schemas.microsoft.com/winfx/2006/xaml' can be used only in compiled resource dictionaries."

This is perhaps a bit misleading, I think the problem actually has something to do with using both x:Array and x:Shared. I changed x:Array to an ordinary ArrayList and it worked fine:

<Window.Resources>

<c:ArrayList xmlns:c="clr-namespaceTongue Tiedystem.Collections;assembly=mscorlib" x:Key="newMenuItems" xTongue Tiedhared="False">

<MenuItem Header="..." />

</c:ArrayList>

</Window.Resources>