3
votes

When using ApplicationCommands class I get a lot of things for free. Text, shortcut and localisation. However there seems to be (or I haven't found it) a way to specify the Character you would specifiy by the _ notation (e.g. E_xit). Is there a way to specify that character for instance for the

ApplicationCommands.New ? 

Or is the only solution to this using a

CustomRountedUICommand 

where I can simply specify _New for the name property?

EDIT:

To clarify here an extraction of my xaml file:

<MenuItem Name="NewProject" Command="{Binding MenuNewProject}" />

MenuNewProject is an ApplicationCommand.New with an InputGesture added. But how to add the underscore without setting the menu header (which is already done by the command binding)?

Edit2:

As it has been pointed out this is obviously a Menu issue. Now the question is: Is there an alternative to the _ in the text to specify the accelerator key? I haven't found anything in the MenuItem class.

Final solution:

Either use:

AccessKeyManager.Register('N',MenuItem)

but loose the shown underlined N or set

MenuItem.Header ='_New'

manually and loose the localization. Unfortunately parsing ApplicationCommands.New.Name always gives back the English name. There might be a solution with the ContentPresenter class but that is a little bit overshot for my small project.

2
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". - John Saunders

2 Answers

1
votes

Use an underscore _ in the Header text to create a hotkey or use a InputBinding to create a shortcut (from this answer):

<Window.CommandBindings>
        <CommandBinding Command="New" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
        <KeyBinding Key="N" Modifiers="Control" Command="New"/>
</Window.InputBindings>
1
votes

RoutedUICommand's do not specify an accelerator key or shortcut key because they may be placed in multiple locations, potentially with different parameters (making them in effect different commands). Thus, the onus for applying accelerators is on the Menu which hosts the commands. You'll need to assign these yourself.

<MenuItem Header="_New"
          Command="ApplicationCommands.New" />

The shortcut key you specify using an InputBinding in the XAML or in Code Behind.

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

The shortcut key should show up in your menu automatically.