Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind any key pressed to command in VM WPF

I'm trying to bind any keyboard key pressed to a command in the ViewModel.

I know that I can bind a specific key, using:

<Window.InputBindings>
    <KeyBinding Command="{Binding ChangeIdCommand}" Key="B"/>
</Window.InputBindings>

Can I bind all key presses to ChangeIdCommand without having to type them all manually?

like image 573
Idanis Avatar asked Jul 18 '16 09:07

Idanis


1 Answers

Try this after your window definition:

<Window x:Class="wpfApplication.MainWindow"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" ...>

<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyDown">
        <i:InvokeCommandAction Command="{Binding ChangeIdCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
like image 173
Jose Avatar answered Nov 08 '22 20:11

Jose