Share via


How to: Create a Shared ContextMenu

You can share a ContextMenu with more than one control by defining the ContextMenu as a resource and setting the ContextMenu property of the controls to a reference of the ContextMenu. You can specify whether the controls share the same ContextMenu or each control has its own ContextMenu by setting the x:Shared Attribute property.

Example

The following example creates a ContextMenu as a resource and assigns the references to four controls. The example sets x:Shared Attribute on the ContextMenu to true, so the four controls share the same instance of the ContextMenu. You can verify this by checking the first MenuItem in the ContextMenu of one control and seeing that the MenuItem is checked in the ContextMenu of the other controls.

<ContextMenu x:Key="SharedInstanceContextMenu" x:Shared="True">
  <MenuItem Header="This MenuItem is checkable" IsCheckable="True" />
  <Separator/>
  <MenuItem Header="This is a regular MenuItem" />
</ContextMenu>


...


<Button Margin="0,5,0,0" Background="LightBlue"
    Content="This Button has a ContextMenu"
    ContextMenu="{DynamicResource SharedInstanceContextMenu}" />
<Button Background="Pink"
    Content="This Button has the same ContextMenu"
    ContextMenu="{DynamicResource SharedInstanceContextMenu}" />
<CheckBox BorderBrush="Red"
    Content="This Check Box has the same ContextMenu"
    ContextMenu="{DynamicResource SharedInstanceContextMenu}" />
<CheckBox BorderBrush="Green" 
    Content="This Check Box has the same ContextMenu"
    ContextMenu="{DynamicResource SharedInstanceContextMenu}" />

The following example creates a ContextMenu as a resource and assigns the references to four controls. The example sets x:Shared Attribute on the ContextMenu to false, so a new instance of the ContextMenu is used for each control. You can verify this by checking the first MenuItem in the ContextMenu of one control and seeing that the MenuItem is not checked in the ContextMenu of the other controls.

<ContextMenu x:Key="NonsharedInstanceContextMenu" x:Shared="False">
  <MenuItem Header="This MenuItem is checkable" IsCheckable="true" />
  <Separator/>
  <MenuItem Header="This is a regular MenuItem" />
</ContextMenu>


...


<Button Background="LightBlue" Margin="0,5,0,0"
    Content="This Button has a ContextMenu"
    ContextMenu="{DynamicResource NonsharedInstanceContextMenu}" />
<Button Background="Pink"
    Content="This Button has the same ContextMenu"
    ContextMenu="{DynamicResource NonsharedInstanceContextMenu}" />
<CheckBox BorderBrush="Red"
    Content="This Check Box has the same ContextMenu"
    ContextMenu="{DynamicResource NonsharedInstanceContextMenu}" />
<CheckBox BorderBrush="Green" 
    Content="This Check Box has the same ContextMenu"
    ContextMenu="{DynamicResource NonsharedInstanceContextMenu}" />

For the complete sample, see ContextMenu Shared Among Controls Sample.

See Also

Tasks

How to: Enable a ContextMenu on a Disabled Control

Concepts

ContextMenu Overview

Reference

Menu

MenuItem