c# - 在 mvvm 中的一个按钮上实现 ctrl+shift+MouseOver
问题描述
我正在尝试在 wpf、mvvm 中的按钮上实现 ctrl+shift+mouseover。我正在做类似的事情:
<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.
任何帮助表示赞赏。
解决方案
您可以实现一个附加行为,将事件处理程序连接到MouseEnter
andMouseLeftButtonDown
事件,并检查在引发事件时是否按下了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);
}
}
推荐阅读
- ionic-framework - Ionic 5 PWA 条带集成 - 浏览器中的 SameSite cookie 问题
- python-3.x - Scapy 和 Docker:使用容器 ID 获取容器的 IP 地址?
- python - 使用 aiohttp 库登录网站
- php - Laravel 命名空间更改为 App\Models 后的包问题
- javascript - 需要 Javascript 信息
- typescript - @types/hapi__hapi 错误 TS2314:通用类型“侦听器”需要 1 个类型参数
- http - 在单页应用程序中定义动态内容安全策略
- winbugs - 为什么错误说模型需要左箭头或 WinBUGS 模型旋转?
- kubernetes - 如何在 k8s 中自定义资源?
- javascript - 在 Web 表单中将单选按钮呈现为可切换按钮的最佳方式