Share via


How to: Create a Shared ContextMenu

This example shows how to create a ContextMenu that you can associate with more than one control.

The following creates a ContextMenu as a resource that is connected to Button and CheckBox controls. Notice that the IsCheckable property is set to true. After you check any of the controls, all the controls share the checked state.

Example

<StackPanel.Resources>

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

</StackPanel.Resources>
 
<TextBlock FontSize="24">Shared ContextMenu</TextBlock>
<Button Background="LightBlue"
    Content="This Button has a ContextMenu"
    ContextMenu="{DynamicResource MyContextMenu}" />
<Button Background="Pink"
    Content="This Button has the same ContextMenu"
    ContextMenu="{DynamicResource MyContextMenu}" />
<CheckBox BorderBrush="Red"
    Content="This Check Box has the same ContextMenu"
    ContextMenu="{DynamicResource MyContextMenu}" />
<CheckBox BorderBrush="Green" 
    Content="This Check Box has the same ContextMenu"
    ContextMenu="{DynamicResource MyContextMenu}" />

For the complete sample, see Shared Context Menu Sample.

See Also

Tasks

How to: Use the ContextMenuService

Reference

Menu
MenuItem

Concepts

ContextMenu Overview