如何在复合WPF中将按键与DelegateCommand相关联

如何在复合WPF中将按键与DelegateCommand相关联

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

问题描述

我正在使用CAL / Prism构建复合应用程序。主区域是一个制表符控件,其中包含多种类型的视图。每个视图都有一个自定义集合命令,它可以处理它们绑定到窗口顶部的工具栏按钮。我之前在非CAL应用程序中通过简单地设置InputBinding的命令,但我没有能够找到CAL模块的源代码中的任何这样的机制。


$我的问题是,什么是最好的方式来联接一个击键到我的视图,以便当用户按 + ,关联DelegateCommand对象处理呢?挂起快捷方式不能是困难的...

解决方案

有一个类名为 CommandReference ,这将允许您使用对命令的引用作为键绑定。

 < 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中引用您的viewmodel中的命令。


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.

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

解决方案

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>

This'll do it.

Edit: 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.

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

08-15 14:17