在WPF视图模型绑定键盘快捷键的命令 [英] Bind a keyboard shortcut to a command in the view model in WPF

查看:204
本文介绍了在WPF视图模型绑定键盘快捷键的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我有一个用户控件在它的工具栏。此工具栏上有一个按钮执行这反过来又绑定到的ICommand 派生类的视图模型公开为它的命令。酒店

I have an application where I have a UserControl with a toolbar in it. This toolbar has a button Execute which in turn has its command bound to an ICommand derived class that the viewmodel exposes as a property.

<Button Grid.Row="0" Command="{Binding ExecuteCommand}">Execute</Button>

现在,我想这个绑定到一个键盘快捷键( F5 )为好。这需要因为是在用户控件的背景下的约束,这是只适用如果用户控件是目前可见的。

Now, I'd like to bind this to a keyboard shortcut (F5) as well. This needs to be bound in the context of the UserControl since, it's only applicable if this usercontrol is visible at the moment.

另一种选择是将其绑定到的KeyDown 实际包含执行文本的文本框,但我真的不稳固,当谈到如何路由事件从控制,而无须在用户控件的代码隐藏真难看黑客的视图模型的命令。

Another option is to bind it to the KeyDown of the textbox that actually contains the text to execute, but I'm really shaky when it comes to how to route the event from the control to the command in the viewmodel without really ugly hacks in the code-behind of the usercontrol.

任何指针感激!

推荐答案

有是消失出于某种原因另一个答案。这工作得很好:

There was another answer that disappeared for some reason. This worked fine:

<UserControl.InputBindings>
  <KeyBinding Key="F5" Command="{Binding ExecuteCommand}" />
</UserControl.InputBindings>



我想给信贷那家伙如果可能的话。请再次出现:)

I'd like to give credit to that guy if possible. Please appear again :)

这篇关于在WPF视图模型绑定键盘快捷键的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆