tags:

views:

903

answers:

3

I have a defined MenuItem that I would like to share between two different menus on one page. The menu contains functionallity that is the same between both menus and I do not want two copies of it. Is there anyway to define a MenuItem in the Page.Resources and reference it in the ContextMenu XAML below?

<Page.Resources>
    <MenuItem x:Key="123"/>
</Page.Resources>

<ContextMenu>
    <MenuItem>Something hardcoded</MenuItem>

    <!-- include shared menu here -->

</ContextMenu>
+1  A: 

Because you want to mix-and-match... I would make a custom control that inherits from ContextMenu that has a "SharedMenuItems" Dependancy Property and a MenuItems Dependancy Property. This way your control can decide how to merge these two sets together. If you would like an example of this, please let me know.

Phobis
While this didn't answer my exact question, it seems like a good work around. I will try it out. Thanks.
Shaun Bowe
Let me know if you have any issues.. I could post code/xaml to do this.
Phobis
+3  A: 

I've done this by setting x:Shared="False" on the menu item itself. Resources are shared between each place that uses them by default (meaning one instance across all uses), so turning that off means that a new "copy" of the resource is made each time.

So:

<MenuItem x:Key="myMenuItem" x:Shared="False" />

You'll still get a "copy" of it, but you only need to define it in one place. See if that helps. You use it like this within your menu definition:

<StaticResource ResourceKey="myMenuItem" />
Matt Hamilton
A: 

A number of options: a) Databind the ContextMenu or the Menu to the same underlying collection and use item templates et al to the work b) Use commands, and databind to a set of command bindings

dhopton