wpfxamlcontextmenuismouseover

Bring up ContextMenu when IsMouseOver on a Button using only XAML


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.


Solution

  • Try to apply "Setter" for a ContextMenu within the ControlTemplate, by providing it's name in the "TargetName" property. For example:

    <Button Width="100" Height="100" x:Name="btn">
            <Button.Style>
                <Style TargetType="Button">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border CornerRadius="2" BorderThickness="3" BorderBrush="DarkGray" x:Name="border">
                                    <Border.ContextMenu>
                                        <ContextMenu x:Name="cmenu">
                                            <TextBlock>Information</TextBlock>
                                        </ContextMenu>
                                    </Border.ContextMenu>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="ContextMenu.IsOpen" Value="True" TargetName="cmenu"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Button.Style>