I have an application where I have a UserControl
with a toolbar in it. This toolbar has a button Execute
which in turn has its command bound to an ICommand
derived class that the viewmodel exposes as a property.
<Button Grid.Row="0" Command="{Binding ExecuteCommand}">Execute</Button>
Now, I'd like to bind this to a keyboard shortcut (F5
) as well. This needs to be bound in the context of the UserControl
since, it's only applicable if this usercontrol is visible at the moment.
Another option is to bind it to the KeyDown
of the textbox that actually contains the text to execute, but I'm really shaky when it comes to how to route the event from the control to the command in the viewmodel without really ugly hacks in the code-behind of the usercontrol.
Any pointers are appreciated!
To assign a keyboard shortcut do the following: Begin keyboard shortcuts with CTRL or a function key. In the Press new shortcut key box, press the combination of keys that you want to assign. For example, press CTRL plus the key that you want to use.
The command is the action to be executed. The command source is the object which invokes the command. The command target is the object that the command is being executed on. The command binding is the object which maps the command logic to the command.
There was another answer that disappeared for some reason. This worked fine:
<UserControl.InputBindings>
<KeyBinding Key="F5" Command="{Binding ExecuteCommand}" />
</UserControl.InputBindings>
I'd like to give credit to that guy if possible. Please appear again :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With