绑定中继命令 [英] Keybinding a RelayCommand

查看:76
本文介绍了绑定中继命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用 RelayCommand .将代码放入视图模型非常好,但是如何将击键绑定到我的命令?

I'm using the RelayCommand in my app. It's great for putting the code in the viewmodel, but how do I bind keystrokes to my command?

RoutedUICommand具有其InputGestures属性,当我按下按键时,该属性将自动调用该命令. (作为一项额外的奖励,它甚至可以在MenuItem中显示击键.)不幸的是,没有RoutedUICommand的额外属性可重用的接口,因此我无法使RelayUICommand拥有相同的魔力.

RoutedUICommand has its InputGestures property, which makes the command automatically be invoked when I press the keystroke. (As an added bonus, it even makes the keystroke display in the MenuItem.) Unfortunately, there's no reusable interface for RoutedUICommand's extra properties, so I can't make a RelayUICommand that gets the same magic.

我已经尝试使用InputBindings:

I've already tried using InputBindings:

<Window.InputBindings>
    <KeyBinding Key="PageUp" Command="{Binding SelectPreviousLayerCommand}"/>
</Window.InputBindings>

但是那给了我一个运行时异常,因为KeyBinding.Command不是依赖项属性. (实际上,它抱怨的是KeyBinding甚至不是DependencyObject.)而且由于我的RelayCommand是ViewModel的一个属性(与RoutedUICommand设计用于的静态字段相反),所以数据绑定是我所知道的唯一方法从XAML引用它.

But that gets me a runtime exception, because KeyBinding.Command isn't a dependency property. (Actually, what it complains about is that KeyBinding isn't even a DependencyObject.) And since my RelayCommand is a property on my ViewModel (as opposed to the static field that RoutedUICommand is designed for), databinding is the only way I know of to reference it from XAML.

你们如何解决这个问题?将击键绑定到RelayCommand的最佳方法是什么?

How have you guys solved this? What's the best way to bind a keystroke to a RelayCommand?

推荐答案

KeyBinding类的Command属性不支持数据绑定.该问题将在.NET 4.0中解决,并且您应该能够在即将发布的.NET 4.0 Beta 2版本中看到它.

The Command property of the KeyBinding class doesn't support data binding. This issue is going to be solved in .NET 4.0 and you should be able to see it in the coming .NET 4.0 Beta 2 version.

这篇关于绑定中继命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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