1

I want to pop up a context menu when the user LEFT clicks a button, and I want the menu to appear above the button. When the user right clicks the button it should do the same thing. Here is the XAML I use to try to do that:

<Button Grid.Column="0" Width="32" Height="32"  ContextMenuService.Placement="Top">
    <Image Source="/Images/config.png" Width="18" Height="18" Stretch="Fill" />
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                    <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
                <Setter Property="ContextMenu">
                    <Setter.Value>
                            <ContextMenu>
                                <MenuItem Header="Play Sounds" IsCheckable="True" IsChecked="{Binding PlaySounds, Mode=TwoWay}" />
                                <MenuItem Header="Always On Top" IsCheckable="True" IsChecked="{Binding OnTop, Mode=TwoWay}" />
                            </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
    </Button>

I set a breakpoint on the "PlaySounds" get/set properties to make sure this is being respected...

When I RIGHT CLICK on the icon, everything works fine. The context menu pops up above the button and the PlaySounds 'get' property is called. However if I instead LEFT CLICK on the icon, the menu pops up where the icon was clicked...not above the button as it should, and the "PlaySounds" and "OnTop" get properties are not called...

Does anyone have any idea how to fix this so that LEFT click does the right thing here?

2
  • possible duplicate of wpf context menu left-click Commented May 2, 2014 at 20:45
  • Right click is expected behavior, see duplicate link for suggestion. Commented May 2, 2014 at 20:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.