我正在学习WPF中的MVVM和Commands。我有几个按钮,我想根据鼠标左右键的点击来触发类似的命令。
到目前为止,我使用了事件处理程序,并且通过检查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}"/>
目前该命令仅在单击左鼠标按钮时触发。如果该命令也会在单击右鼠标按钮时触发,并且我可以找出单击的是哪个按钮,那将是另一种解决方案。
我搜索并找到了这个使用装饰器的问题和答案。如何在WPF的ControlTemplate中将命令绑定到右鼠标按钮等? 我尝试过,但据我所知,这对我的按钮不起作用。
有什么建议吗?