没有重点的键绑定 [英] Keybindings without any focus

查看:73
本文介绍了没有重点的键绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF屏幕,其中有六个按钮。我想将每个键绑定与关联。它们都是通过MVVM由ICommand驱动的。

I have a WPF screen that has half a dozen buttons. I'd like to associate each with a keybinding. They are all ICommand driven via MVVM.

我目前具有绑定到事件的绑定,而不是实际的按钮:

I currently have the keybindings bound to the events and not the actual buttons:

<UserControl.InputBindings>
        <KeyBinding Key="N"  Command="{Binding MyCommand}"/>

但是,要使任何键绑定正常工作,我必须至少设置1个按钮以将焦点放在后面的代码中。我真的不想这样做,因为每个按钮/命令是否激活都具有不同的规则,并且我希望焦点动画在页面加载时不处于激活状态。

However, for any Keybinding to work I have to set at least 1 button to focus in the codebehind. I'd really like to not do this because each button/command has different rules on if it's active or not and I have focus animation that I'd prefer wasn't active on page load.

这可能吗?还是我必须设置焦点?

Is this possible or do I have to set focus?

推荐答案

您只需要将usercontrol的focusable设置为true,因为某些元素默认情况下不为真

You just need to set usercontrol's focusable to true, because some element doesn't have it to true by default

<UserControl Focusable="True">

然后您必须在用户控件的加载事件中设置this.Focus()

Then you have to set this.Focus() in the loaded event of the user control

这篇关于没有重点的键绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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