I have an MVVM application in which the MainWindow contains a grid with several views.
In one of the viewmodels there is a command which I can activate by using hot keys in its corresponding view. I can only activate the command when I am placed in the part of the MainWindow that contains the specific view.
The following code works fine, if I only want to be able to activate the command in the specific view:
ComponentView.xaml:
...
<UserControl.InputBindings>
<KeyBinding Gesture="CTRL+U" Command="{Binding Path=UploadCmd}"/>
</UserControl.InputBindings>
</UserControl>
I would like to be able to activate the command by using the hot keys from any part of the MainWindow.
This is my failed attempt to do the keybinding in the MainWindow, so that the command can be activated from anywhere:
MainWindow.xaml:
<Window x:Class="MyProgram.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:MyProgram.View"
xmlns:vm="clr-namespace:MyProgram.ViewModel"
...>
<Grid>
// Grid content
</Grid>
<Window.DataContext>
<vm:ComponentViewModel>
<KeyBinding Gesture="CTRL+U" Command="{Binding Path=UploadCmd}"/>
</vm:ComponentViewModel>
</Window.DataContext>
</Window>
Is it possible to somehow let the application know where the command is directly in the xaml?