On every ICommandSource
you can set the CommandTarget
Property. In the case of RoutedUICommand
s it means, that this Target will be used instead of the KeyBoard.FocusedElement
.
So for Example take this Command:
public static class MyCommands
{
static MyCommands()
{
Test = new RoutedUICommand("Test Command", nameof(Test), typeof(MyCommands));
Test.InputGestures.Add(new KeyGesture(Key.Escape));
}
public static RoutedUICommand Test { get; }
}
So the CommandTarget could be set like this:
<Button Command="{x:Static local:MyCommands.Test}" CommandTarget="{Binding }" />
However if the the User pressed the Escape Button, the KeyBoard.FocusedElement
will be used as CommandTarget
.
How can I change the CommandTarget
when a RoutedUICommand
is activated via KeyGesture
?