Question

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>
Was it helpful?

Solution

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" />

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top