【问题标题】:Implement ctrl+shift+MouseOver on a button in mvvm在 mvvm 中的一个按钮上实现 ctrl+shift+MouseOver
【发布时间】:2018-07-18 06:38:53
【问题描述】:

我正在尝试在 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.

感谢任何帮助。

【问题讨论】:

  • 你想达到什么目的?如果出现 Ctrl+Shift+MouseOver 应该怎么办?
  • MouseOver 不需要按下按钮吗?你怎么能在没有鼠标悬停的情况下按下按钮? “回车键?会有什么区别?
  • 写一个行为来检查你的鼠标事件键修饰符。
  • 我在 Ctrl+Shift+LeftClick 上绑定了一个不同的命令,在 Ctrl+Shift+MouseOver 上绑定了一个不同的命令。这两个命令是独立的,用于不同的目的。

标签: c# wpf mvvm prism-6


【解决方案1】:

您可以实现一个attached behaviour,将事件处理程序连接到MouseEnterMouseLeftButtonDown 事件,并检查 CtrlShift 键是否被按下引发事件的时间,例如:

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);
    }
}

【讨论】:

  • 我需要以纯MVVM方式实现。
  • 这就是为什么我建议你应该创建一个附加的行为。那是纯 MVVM。那你的意思是什么?
  • 如果你不知道附加行为是什么,你应该点击我提供的链接。听起来您还想阅读更多有关 MVVM 的信息……您不会使用输入绑定和交互触发器来解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多