Input Bindings in WPF 4.0
Feb 2
2011
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.


Comments