MVVM is leaking into the framework. This is a good thing.

Input Bindings in WPF 4.0

In WPF 3.0 and 3.5, if you wanted to attach input gestures (either key presses or mouse moves) to commands, you had to statically create the command first, and create the input binding, something like this:

<Window 
    xmlns:local="clr-namespace:WpfApplication1" 
    ...>
    <Window.InputBindings>
        <KeyBinding Gesture="Ctrl+D" Command="local:CustomCommands.DonkeyCommand" />
    </Window.InputBindings>
</Window>

In WPF 4.0, the Command property on KeyBinding (or rather, on the base class InputBinding) is now a dependency property, meaning we can MVVM-ify this, like:

<Window 
    ...>
    <Window.InputBindings>
        <KeyBinding Gesture="Ctrl+D" Command="{Binding DonkeyCommand}" />
    </Window.InputBindings>
</Window>

Where DonkeyCommand is a public property on the ViewModel. Probably asks the server to walk children up and down the beach.

inputbinding wpf4
Posted by: Ian Randall
Last revised: 01 Feb, 2011 05:47 p.m.

Comments

Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview