开发者

Command routing for Keyboard shortcuts

开发者 https://www.devze.com 2023-02-03 02:45 出处:网络
Basically I want to create a keyboard shortcut which is valid within the scope of a window, and not just enabled when focus is within the control that binds it.

Basically I want to create a keyboard shortcut which is valid within the scope of a window, and not just enabled when focus is within the control that binds it.

in more detail....

I have a window which has 3 controls:

  • a toolbar
  • a textbox
  • a Custom Control

The toolbar has a button bound to the Command CustomCommands.CmdA, and linked to keyboard shortcut Ctrl+T.

My Custom Control can process CmdA. W开发者_运维知识库hen I run the app and click on my custom control CmdA is enabled and works fine. Also Ctrl+T causes the command to fire.

However when I select the text box, my custom command CmdA becomes disabled.

I can rectify this by setting the command target for CmdA's button. Now when I select the textBox, CmdA is still enabled.

But the Keyboard shortcut Ctrl+T does nothing.

Is there any easy way to change the scope of keyboard shortcuts? Or do I need to catch the keypress somewhere lower down, and work out which Command it relates to and route it myself (if so is there a framework within which to do this?)


For that you normally just need to specify the input bindings in the window, e.g.:

 <Window.InputBindings>
        <KeyBinding Key="N" Modifiers="Control" Command="New"/>
        ...
 </Window.InputBindings>

For built-in application commands (which are RoutedCommands) you need CommandBinding in the window as well.

<Window.CommandBindings>
        <CommandBinding Command="New" Executed="CommandBinding_Executed" />
        ...
</Window.CommandBindings>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号