Create a ContextMenu that can be associated with more than one control.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="Width">
<Window.Resources>
<ContextMenu x:Key="SharedInstanceContextMenu" x:Shared="True">
<MenuItem Header="This MenuItem is checkable" IsCheckable="True" />
<Separator/>
<MenuItem Header="This is a regular MenuItem" />
</ContextMenu>
<ContextMenu x:Key="NonsharedInstanceContextMenu" x:Shared="False">
<MenuItem Header="This MenuItem is checkable" IsCheckable="true" />
<Separator/>
<MenuItem Header="This is a regular MenuItem" />
</ContextMenu>
</Window.Resources>
<StackPanel Orientation="Horizontal" >
<StackPanel Margin="5">
<TextBlock TextWrapping="WrapWithOverflow" Width="400" FontSize="12">
<Run FontSize="24">Shared ContextMenu </Run>
<LineBreak/>
<Bold>True</Bold> control.
</TextBlock>
<Button Margin="0,5,0,0" Content="Button"
ContextMenu="{DynamicResource SharedInstanceContextMenu}" />
<Button Content="Button"
ContextMenu="{DynamicResource NonsharedInstanceContextMenu}" />
<CheckBox Content="Check Box"
ContextMenu="{DynamicResource SharedInstanceContextMenu}" />
<CheckBox Content="Check Box"
ContextMenu="{DynamicResource NonsharedInstanceContextMenu}" />
</StackPanel>
</StackPanel>
</Window>
Related examples in the same category