0

On every ICommandSource you can set the CommandTarget Property. In the case of RoutedUICommands 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?

LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
  • Have you thought of using `RelayCommand` or `DelegateCommand` instead? And use `CommandParameter`. – XAMlMAX Oct 24 '17 at 07:25
  • 1
    Yes I did, but in my case It doesn't make sense. The `CommandTarget` needs to be set, To change the routing behavior, so passing a Parameter won't solve the issue. – LuckyLikey Oct 24 '17 at 07:40
  • Does that even makes sense to have command target in this case? You pressed Escape key, you have some element focused, what can be target instead of that element? Why do you need that? – Evk Oct 24 '17 at 08:06
  • @Evk I am implementing a Tabbed MDI, where Tabs can be Dragged out into a floating Tool-Window mode. So the `CommandBinding`s are inside a `UserControl` when sometimes the parent Panel is focused, while pressing the button. So the Target needs to be manually set to the Content. I already did thiis with any Menu and Contextmenu Button. But I don't get how I can do the same with KeyGestures. – LuckyLikey Oct 24 '17 at 08:13
  • So parent panel is focused, user presses Esc key. You want somehow route this esc key to the child UserControl which has command binding for it? – Evk Oct 24 '17 at 08:17
  • @Evk Actually I already did it like this for MenuButtons and ContextMenu `CommandTarget="{Binding ActivePane.Content}"`. Really I only need to somehow set this behavior to `KeyGestures`. – LuckyLikey Oct 24 '17 at 08:20
  • Yes but with menus it's different - you actually have something user clicks on, so at the moment of click command is executed and can indeed choose any target. But escape key can be pressed anywhere, it's global. That's why I say that it does not make much sense to have command target for it. If forget about commands the goal is to trigger something on escape key in child UserControl when parent Panel is focused, right? – Evk Oct 24 '17 at 08:25
  • @evk yes indeed. – LuckyLikey Oct 24 '17 at 08:30
  • What about ``? Add this to InputBindings of your panel and remove that `Test.InputGestures.Add(new KeyGesture(Key.Escape));` – Evk Oct 24 '17 at 09:04
  • @Evk thanks for this Idea. I did this, the only problem I had, was that the pane itself having the focus didn't enable me to trigger the command, allthough the target was set correctly. - However thanks anyway. I think i'll stop it here. – LuckyLikey Oct 24 '17 at 12:56

0 Answers0