鼠标右键的WPF按钮命令?

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 的问答。 How do I bind a command to e.g. the right mouse button in a ControlTemplate in WPF? 我试过了,但据我所知这对我的按钮不起作用。

有什么建议吗?

试试这个

<Button Content="Command Test">
    <Button.InputBindings>
        <MouseBinding Gesture="RightClick" Command="{Binding PressLetterCommand}" />
    </Button.InputBindings>
</Button>