Set KeyBinding on a whole WPF window

I working on an MVVM window and want to control something in the view model by the keyboard, but if i place the following code directly under the window it can't be compiled only if i place under for example a text box. How can i do this?

   <KeyBinding Key="P" Command="{Binding ToggleCommand}"/>

Upvotes: 2

Views: 3368

Answers (1)

ChrisO
ChrisO

Reputation: 5465

You need to assign the KeyBinding to the InputBindings property on Window

<Window.InputBindings>
    <KeyBinding Key="P" Command="{Binding ToggleCommand}"/>
</Window.InputBindings>

Upvotes: 2

Related Questions