左键单击WPF上下文菜单 [英] WPF Context menu on left click

查看:83
本文介绍了左键单击WPF上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序.在Xaml文件中有一个Image控件.

I have a WPF application..In which I have an Image control in Xaml file.

在该图像的右键单击上,我有一个上下文菜单.

On right click of this image I have a context menu.

我也希望在左键单击"中显示该内容.

I would like to have same to be displayed on "Left click" also.

我该如何以MVVM方式执行此操作?

How do I do this in MVVM way ?

推荐答案

这是仅XAML的解决方案. 只需将这种样式添加到您的按钮即可. 这将导致右键单击菜单同时打开.享受吧!

Here is a XAML only solution. Just add this style to your button. This will cause the context menu to open on both left and right click. Enjoy!

<Button Content="Open Context Menu">
    <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 />
                        <MenuItem />
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

这篇关于左键单击WPF上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆