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

发布于 2024-08-05 20:12:40 字数 288 浏览 2 评论 0原文

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

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

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 Alt + T, the associated DelegateCommand object handles it? Hooking up a shortcut can't be THAT difficult...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说谎友 2024-08-12 20:12:40

仅供参考,CommandReference 类当前未包含在您可以引用的程序集中,但包含在 MV-VM 项目模板中。因此,如果您不从模板构建应用程序,那么您必须从其他地方获取该类。我选择从示例项目中复制它。我将其包含在下面是为了让每个人都能轻松访问这一小块优点,但请务必在 MV-VM Toolkit 的未来版本中检查模板的更新。

/// <summary>
/// This class facilitates associating a key binding in XAML markup to a command
/// defined in a View Model by exposing a Command dependency property.
/// The class derives from Freezable to work around a limitation in WPF when data-binding from XAML.
/// </summary>
public class CommandReference : Freezable, ICommand
{
    public CommandReference( )
    {
    }
    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( "Command", typeof( ICommand ), typeof( CommandReference ), new PropertyMetadata( new PropertyChangedCallback( OnCommandChanged ) ) );

    public ICommand Command
    {
        get { return (ICommand)GetValue( CommandProperty ); }
        set { SetValue( CommandProperty, value ); }
    }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        if (Command != null)
            return Command.CanExecute( parameter );
        return false;
    }

    public void Execute(object parameter)
    {
        Command.Execute( parameter );
    }

    public event EventHandler CanExecuteChanged;

    private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CommandReference commandReference = d as CommandReference;
        if (commandReference != null)
        {
            ICommand oldCommand = e.OldValue as ICommand;
            if (oldCommand != null)
                oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;

            ICommand newCommand = e.NewValue as ICommand;
            if (newCommand != null)
                newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
        }
    }

    #endregion

    #region Freezable

    protected override Freezable CreateInstanceCore( )
    {
        return new CommandReference();
    }

    #endregion
}

享受!

Just for reference, the CommandReference class is currently not included in an assembly that you can reference, but is included with the M-V-VM project template. So if you don't build your application from the template, then you have to get the class from somewhere else. I chose to copy it from the sample project. I included it below to allow everyone easy access to this little chunk of goodness, but be sure to check for updates to the template in future versions of the M-V-VM Toolkit.

/// <summary>
/// This class facilitates associating a key binding in XAML markup to a command
/// defined in a View Model by exposing a Command dependency property.
/// The class derives from Freezable to work around a limitation in WPF when data-binding from XAML.
/// </summary>
public class CommandReference : Freezable, ICommand
{
    public CommandReference( )
    {
    }
    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( "Command", typeof( ICommand ), typeof( CommandReference ), new PropertyMetadata( new PropertyChangedCallback( OnCommandChanged ) ) );

    public ICommand Command
    {
        get { return (ICommand)GetValue( CommandProperty ); }
        set { SetValue( CommandProperty, value ); }
    }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        if (Command != null)
            return Command.CanExecute( parameter );
        return false;
    }

    public void Execute(object parameter)
    {
        Command.Execute( parameter );
    }

    public event EventHandler CanExecuteChanged;

    private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CommandReference commandReference = d as CommandReference;
        if (commandReference != null)
        {
            ICommand oldCommand = e.OldValue as ICommand;
            if (oldCommand != null)
                oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;

            ICommand newCommand = e.NewValue as ICommand;
            if (newCommand != null)
                newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
        }
    }

    #endregion

    #region Freezable

    protected override Freezable CreateInstanceCore( )
    {
        return new CommandReference();
    }

    #endregion
}

Enjoy!

深巷少女 2024-08-12 20:12:40

MVVM 工具包 有一个类称为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 引用视图模型中的命令。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文