[英]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.