如何在 Prism 4 中启用 DelegateCommand 的 KeyBinding?
我想在 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?
I would like to enable a KeyBinding for a DelegateCommand in a Prism4 desktop application. For example, in my XAML file I have this:
<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>
and in my ViewModel I have this:
public DelegateCommand<string> Command3 { get; private set; }
private void ExecuteCommand3(string commandParameter)
{
Debug.WriteLine("ExecuteCommand3");
}
private bool CanExecuteCommand3(string commandParameter)
{
return true;
}
When I press the HitMe button the debug line outputs but pressing CTRL+A has no effect.
I have considered using the CommandReference class from TestMvvmExample2341 but that seems to duplicate the functionality of Prism 4 mechanisms.
Is there a an easy way to have CTRL+A invoke the Command3 in Prism4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就是这样,也许您的问题与视图中的焦点有关,请尝试以下操作:
在运行时将焦点设置在按钮上,然后应用击键。另请查看这些帖子:
WPF MVVM KeyBinding 无法立即识别且并不总是有效
http://joyfulwpf.blogspot.com/2009/05/mvvm-commandreference-and-keybinding.html
That's it, maybe your problem is related to the Focus in your view, try this:
At runtime set the focus over the Button and then apply the keystroke. Also take a look at these posts:
WPF MVVM KeyBinding not being recognized right away and not always working
http://joyfulwpf.blogspot.com/2009/05/mvvm-commandreference-and-keybinding.html