[英]WPF Button Command for right mouse button?
我正在学习WPF中的MVVM和命令。 我有几个按钮,我想触发类似的命令,具体取决于使用鼠标左键或右键单击按钮的事实。
到目前为止,我使用了事件处理程序,通过检查MouseButtonEventArgs,我能够确定按下了哪个鼠标按钮。
<Button Content="Command Test" PreviewMouseDown="Button_PreviewMouseDown"/>
当前代码背后:
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
if (e.LeftButton == MouseButtonState.Pressed) {
Debug.Print("Left");
}
else {
Debug.Print("Right");
}
}
但是如果我使用命令,我看不到任何类似的东西。
如何为按钮设置不同的命令? 单击鼠标左键时的一个命令和单击鼠标右键时的另一个命令?
<Button Content="Command Test" Command="{Binding PressLetterCommand, Mode=OneWay}"/>
目前,只有在单击鼠标左键时才会触发命令。 如果单击鼠标右键也会触发该命令,我可以找到单击的按钮,这也是一个解决方案。
我搜索了一下,发现这个问题和答案都使用了Decorator。 如何在WPF中的ControlTemplate中将命令绑定到例如鼠标右键? 我尝试过,但据我了解,这对我的按钮不起作用。
有什么建议?
试试这个
<Button Content="Command Test">
<Button.InputBindings>
<MouseBinding Gesture="RightClick" Command="{Binding PressLetterCommand}" />
</Button.InputBindings>
</Button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.