Reputation: 10008
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>
Upvotes: 2
Views: 2635
Reputation: 7292
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
Upvotes: 0
Reputation: 204219
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" />
Upvotes: 6
Reputation: 7754
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.
Upvotes: 1