如何在 Composite WPF 中将按键与 DelegateCommand 相关联? [英] How do I associate a keypress with a DelegateCommand in Composite WPF?

查看:18
本文介绍了如何在 Composite WPF 中将按键与 DelegateCommand 相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CAL/Prism 构建一个复合应用程序.主要区域是一个选项卡控件,其中包含多种类型的视图.每个视图都有一个可以处理的自定义命令集,这些命令绑定到窗口顶部的工具栏按钮.我之前在非 CAL 应用程序中通过简单地在命令上设置 InputBinding 来完成此操作,但我无法在 CAL 模块的源代码中找到任何此类机制.

I am building a composite application using CAL/Prism. The main region is a tab control, with multiple types of views in it. Each view has a custom set commands that it can handle which are bound to toolbar buttons at the top of the window. I've done this before in non-CAL apps by simply setting the InputBinding on the command, but I haven't been able to find any such mechanism in the source code for the CAL modules.

我的问题是,将击键连接到我的视图的最佳方法是什么,以便当用户按下 Alt + T 时,关联的 DelegateCommand 对象处理它?连接快捷方式不会那么困难...

My question is, what is the best way to hook up a keystroke to my view, so that when the user presses Alt + T, the associated DelegateCommand object handles it? Hooking up a shortcut can't be THAT difficult...

推荐答案

MVVM Toolkit 有一个名为 CommandReference 的类,它允许您使用对命令的引用作为键绑定.

The MVVM Toolkit has a class called a CommandReference that will allow you to use a reference to a command as a keybinding.

<Window ...
    xmlns:toolkit="clr-namespace:CannotRememberNamspace;assembly=OrTheAssembly"
    >

    <Window.Resources>
        <toolkit:CommandReference 
                 x:Key="ExitCommandReference" 
                 Command="{Binding ExitCommand}" />
    </Window.Resources>

    <Window.InputBindings>
        <KeyBinding Key="X" 
                    Modifiers="Control" 
                    Command="{StaticResource ExitCommandReference}" />
    </Window.InputBindings>
</Window>

这样就可以了.

编写后,WPF 4.0 修复了这个特定问题,您不再需要使用静态资源解决方法.您可以直接从 KeyBinding 引用视图模型中的命令.

Since this was written, WPF 4.0 fixed this particular issue and you no longer have to use the static resource workaround. You can reference the command in your viewmodel directly from the KeyBinding.

这篇关于如何在 Composite WPF 中将按键与 DelegateCommand 相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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