Imagine situation, where I have user control containing button:
<UserControl>
<Button Click="ShowContextMenu" />
</UserControl>
This button will on the click event display ContextMenu:
ContextMenu contextMenu = this.FindResource("ContextMenuDefinition") as ContextMenu;
contextMenu.PlacementTarget = sender as Button;
contextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
contextMenu.IsOpen = true;
Defined as Resource in the ResourceDictionary (separate file):
<ContextMenu x:Key="ContextMenuDefinition" DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<ContextMenu.Template>
<ControlTemplate>
<Button Style="{StaticResource MyStyle}" Command="{Binding AwesomeCommand}" />
</ControlTemplate>
</ContextMenu.Template>
</ContextMenu>
How can I trigger any AwesomeCommand from this context menu? - how and where should I define it and then pass it to the context menu for invocation?
Many thanks!