【问题标题】:How to enable KeyBinding for DelegateCommand in Prism 4?如何在 Prism 4 中为 DelegateCommand 启用 KeyBinding?
【发布时间】:2011-02-25 01:21:38
【问题描述】:

我想在 Prism4 桌面应用程序中为 DelegateCommand 启用 KeyBinding。例如,在我的 XAML 文件中,我有这个:

<Grid.InputBindings>
        <KeyBinding Gesture="CTRL+A" Command="{Binding Command3}"/>
</Grid.InputBindings>

<StackPanel>
   <Button Grid.Row="0" Grid.Column="1" Content="HitMe" prism:Click.Command="{Binding  Command3}" />
</StackPanel>

在我的 ViewModel 我有这个:

public DelegateCommand<string> Command3 { get; private set; }

    private void ExecuteCommand3(string commandParameter)
    {
        Debug.WriteLine("ExecuteCommand3");

    }

    private bool CanExecuteCommand3(string commandParameter)
    {
        return true;
    }

当我按下 HitMe 按钮时,调试行输出,但按下 CTRL+A 无效。

我曾考虑使用 TestMvvmExample2341 中的 CommandReference 类,但这似乎复制了 Prism 4 机制的功能。

有没有一种简单的方法可以让 CTRL+A 调用 Prism4 中的 Command3?

【问题讨论】:

  • 什么版本的 .NET? 3.0、3.5 还是 4.0?

标签: wpf prism key-bindings delegatecommand


【解决方案1】:

就是这样,可能你的问题和你视图中的Focus有关,试试这个:

在运行时将焦点设置在 Button 上,然后应用击键。也看看这些帖子:

WPF MVVM KeyBinding not being recognized right away and not always working

http://joyfulwpf.blogspot.com/2009/05/mvvm-commandreference-and-keybinding.html

【讨论】:

  • 感谢马斯卡布!就是这样。我在 UserControl_Loaded 事件中添加了对 firstButtonName.Focus() 的调用,这样就不需要制表符了。
  • 您好,Tom,这很好,但是如果您打算将 MVVM 与 Prism 一起使用,那么在代码隐藏中设置焦点可能会破坏 MVVM 模式。我推荐你是使用FocusManager,看看这个帖子link
猜你喜欢
  • 1970-01-01
  • 2019-02-15
  • 2020-12-11
  • 1970-01-01
  • 2014-12-13
  • 2016-03-15
  • 2017-05-12
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多