I am trying to use XAML (only, no codebehind) to bring up the ContextMenu of a button.
I have this my button here
<Button x:Name="btn" Style="{StaticResource mybutton}" >
<Button.ContextMenu>
<ContextMenu>
<TextBlock Text="Information"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
The Style for the button here
<Style TargetType="{x:Type Button}" x:Key="mybutton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ContextMenu.IsOpen" Value="True"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
My google-fu is failing me for what seems like an easy solution. I really would prefer to avoid using codebehind (MouseEnter/MouseLeave events).
Thank you in advance.
Try to apply "Setter" for a ContextMenu within the ControlTemplate, by providing it's name in the "TargetName" property. For example: