Wpf – Keybindings without any focus

c#-4.0mvvmwpfxaml

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}"/>

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?

Best Answer

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

<UserControl Focusable="True">

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

Related Topic