3

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.

Johannes Maria Frank
  • 2,747
  • 1
  • 29
  • 38
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 10 '13 at 12:49

2 Answers2

1

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>
Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115
  • When I use command binding with the ApplicationCommands.New the Header text is automaticly set. The question here is how to add the underscore to that text as the name property is read only. – Johannes Maria Frank May 10 '13 at 12:54
  • 1
    You can still set the Header of the MenuItem. Or you could create your own Commands. You can't change the standard ApplicationCommands as far as I know. – Emond May 10 '13 at 13:02
  • 1
    I guess I am left with the solution to set the header. My current solution is to parse the name of the applicationcommand add my underscore and then set the header. – Johannes Maria Frank May 10 '13 at 15:01
  • don't do that if you want to use localization later on – Emond May 10 '13 at 15:24
  • Yes, I know but I have to choose: loose the user hint or have to manage localization myself. Do you have an idea how to retrieve the localized name of the command? – Johannes Maria Frank May 10 '13 at 17:10
  • See this conversation: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9d3f9534-4a10-4860-90b5-d2ea5c967fca Note that you would need to have the correct .NET runtime language installed. – Emond May 10 '13 at 17:33
  • Thank you I already read this conversation. Nevertheless even if I have e.g. German installed and it shows up in german, the ApplicationCommands.New.Name would still evaluate to "New" instead of "Neu". Showing the name as localized in a menu seems to be different from retrieving that name form within the code. – Johannes Maria Frank May 11 '13 at 19:13
  • You might need to set the culture of the thread you are using to retrieve the value. So far I have always created my own commands and put all the string that needed translation in satelite assemblies. I dislike mixing custom commands and standard commmands, but that's just me. – Emond May 11 '13 at 19:46
1

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.

user7116
  • 63,008
  • 17
  • 141
  • 172
  • Some ApplicationCommands do have a shortcut some not. But the all lack the _ shortcut like _File so I can access a menu item like New with Alt, F, N. I do not mean Ctrl-N. – Johannes Maria Frank May 10 '13 at 12:57
  • 1
    @JohannesFrank: those are Accelerator Keys, and as I said you have to set it yourself. – user7116 May 10 '13 at 13:01
  • Ok, I understood, this has nothing to do with the command. I've studied the MenuItem class but couldn't find anything like an accelerator key property. Is there an alternative method besides the underscore? – Johannes Maria Frank May 10 '13 at 13:13
  • 1
    @JohannesFrank: not that I'm aware. You still need the underscore and either `RecognizesAccessKey="True"` (on a `ContentControl`) or an `_New` entity of sorts. – user7116 May 10 '13 at 13:16
  • Your hint and google lead me to the AccessManager where I could register a key. This works however does not show up in the text (not underlined). I voted you up for your hint. – Johannes Maria Frank May 10 '13 at 14:58