2

We have a case where we have two externally-defined RoutedUICommand objects (i.e. we don't own them thus we can't change them) which come pre-wired to the same CTRL+N key binding. We can address that in our code by manually setting the InputBindings we want, like so (Note: AddMany is my convenience extension that takes a param array):

private void InitializeCommands(){

    // Both NewFooCommand and NewLaaCommand define CTRL-N as their shortcut,
    // so we have to explicitly assign the shortcuts we want for each
    // Note: You should explicitly specify both as the system may 'choose' the wrong one to
    // handle the default case.  i.e. we can't just specify NewLaaCommand for the new shortcut
    // because the system may also wire up CTRL+N to NewLaaCommand so it would now respond to
    // both shortcuts while NewFooCommand now wouldn't respond to either.

    InputBindings.AddMany(
        new InputBinding(NewFooCommand, new KeyGesture(Key.N, ModifierKeys.Control)),
        new InputBinding(NewLaaCommand, new KeyGesture(Key.N, ModifierKeys.Control | ModifierKeys.Alt))
    );

    CommandBindings.AddMany(
        new CommandBinding(NewFooCommand, (s, e) => MessageBox.Show("New Foo!")),
        new CommandBinding(NewLaaCommand, (s, e) => MessageBox.Show("New Laa!"))
    );
}

This works fine and addresses that part of our issue so we can now use shortcuts as we see fit.

The issue we're still trying to address is we have global styles for buttons which automatically apply their tool tips to show their assigned shortcut keys. It's based on what's specified on the command itself, which in this case is of course CTRL+N so that's what they both show, even though one is now CTRL+ALT+N thanks to the manually-specified InputBinding.

Now while we could go around and hard-code the tool tips, I'm wondering if there's a way to instead of asking the command what it's binding/bindings is/are, if we can somehow query the system to see which current key bindings are in effect relative to a given control (i.e. the button.) However, I'm not sure how to get that information without having to manually walk the visual tree searching InputBinding collections for the specific RoutedUICommand.

Is it possible to ask the Input system which keybinding is actually in effect, not just which one is defined on the RoutedUICommand?

A 'hack' alternative is to just define our own 'mirror' commands with the correct key bindings so the style correctly updates the tool tip, then we simply delegate most actions to the actual command. Like I said, it's a hack, but it would work.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

0 Answers0