左键单击 WPF 上下文菜单 [英] WPF Context menu on left click
本文介绍了左键单击 WPF 上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 WPF 应用程序..其中我在 Xaml 文件中有一个图像控件.
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屋!
查看全文