首页 > 解决方案 > 在 mvvm 中的一个按钮上实现 ctrl+shift+MouseOver

问题描述

我正在尝试在 wpf、mvvm 中的按钮上实现 ctrl+shift+m​​ouseover。我正在做类似的事情:

<Button.InputBindings>
    <KeyBinding Gesture="Ctrl+Shift" Command="{Binding KeyCommand}" 
CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}" 
/>
</Button.InputBindings>

或类似的东西:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseEnter">
      <i:InvokeCommandAction Command="{Binding MouseOverCommand}" />
    </i:EventTrigger>
 </i:Interaction.Triggers>

由于鼠标手势不支持“MouseEnter”事件,我无法获得所需的行为。我试图以如下方式实现:

Step 1.- Through keybinding setting a variable in the viewmodel.

Step 2.- Accessing that variable's value on MouseEnter and do whatever in the command.

任何帮助表示赞赏。

标签: c#wpfmvvmprism-6

解决方案


您可以实现一个附加行为,将事件处理程序连接到MouseEnterandMouseLeftButtonDown事件,并检查在引发事件时是否按下了Ctrland键,例如:Shift

private void OnMouseEnter(object sender, MouseEventArgs e)
{
    if ((Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
        && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
    {
        TheCommand.Execute(null);
    }
}

推荐阅读