[英]WPF Button Command for right mouse button?
I am learning about MVVM and Commands in WPF. 我正在学习WPF中的MVVM和命令。 I have a couple of buttons and I want to trigger similar commands depending on the fact if the buttons are clicked with the left or right mouse button. 我有几个按钮,我想触发类似的命令,具体取决于使用鼠标左键或右键单击按钮的事实。
Until now I used Event Handlers and I was able to determine which mouse button was pressed by checking the MouseButtonEventArgs. 到目前为止,我使用了事件处理程序,通过检查MouseButtonEventArgs,我能够确定按下了哪个鼠标按钮。
<Button Content="Command Test" PreviewMouseDown="Button_PreviewMouseDown"/>
Current code behind: 当前代码背后:
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
if (e.LeftButton == MouseButtonState.Pressed) {
Debug.Print("Left");
}
else {
Debug.Print("Right");
}
}
But I don't see anything similar if I use Commands. 但是如果我使用命令,我看不到任何类似的东西。
How can I set different commands for a button? 如何为按钮设置不同的命令? One command when the left mouse button is clicked and another command when the right mouse button is clicked? 单击鼠标左键时的一个命令和单击鼠标右键时的另一个命令?
<Button Content="Command Test" Command="{Binding PressLetterCommand, Mode=OneWay}"/>
Currently the Command only fires when the left mouse button is clicked. 目前,只有在单击鼠标左键时才会触发命令。 If the command would also be fired if the right mouse button is clicked and I can find out which button was clicked that would be also a solution. 如果单击鼠标右键也会触发该命令,我可以找到单击的按钮,这也是一个解决方案。
I searched and I found this question and answer which uses Decorator. 我搜索了一下,发现这个问题和答案都使用了Decorator。 How do I bind a command to eg the right mouse button in a ControlTemplate in WPF? 如何在WPF中的ControlTemplate中将命令绑定到例如鼠标右键? I tried it but as far as I understand this won't work for my buttons. 我尝试过,但据我了解,这对我的按钮不起作用。
Any suggestions? 有什么建议?
Try this 试试这个
<Button Content="Command Test">
<Button.InputBindings>
<MouseBinding Gesture="RightClick" Command="{Binding PressLetterCommand}" />
</Button.InputBindings>
</Button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.