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.
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>
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.
Related
I'm creating a bidding program to give customers quotes. I'm using WPF + MVVM
What I would like to do is to be able to create a shortcut (Ctrl+O) from the main page directly to the view that enables me to select which bid I want to open.
I can't imagine it is very difficult, I just seem to be missing something. I've looked for the answer in other threads. It is possible that I have missed it, if I have, please let me know and refer me to where the answer can be found.
Assuming you already have a command you are calling when clicking the button to open the other view you could use an InputBinding
Something like this should do the work
<Window>
<Window.InputBindings>
<KeyBinding Command="{Binding OpenSecondViewCommand}"
Modifiers="Control"
Key="O"/>
</Window.InputBindings>
</Window>
I defined a keybinding in my XAML-code:
<KeyBinding Command="{Binding CopyCommand}" Modifiers="Control" Key="C" />
If i press CTRL+C, the CopyCommand isn't called. If i use the following instead:
<KeyBinding Command="{Binding CopyCommand}" Modifiers="Control" Key="U" />
and press CTRL+U the command works fine.
I think the reason for this is, that the CTRL+C gets handled somewhere else and the Handled value of the KeyEventArgs is set to true, so it doesn't reach my control.
Do I have any possibility to find all consumers for this/any given event? Can I somehow debug/follow an event from the moment it is created until it is consumed?
Simply searching for KeyDown-handlers is not really an option.
I would be fine with using even an external software, e.g. something like Snoop, but would prefer a solution using Visual Studio 2012 or writing code.
Using Snoop I could see that CTRL+U gets handled by the UserControl (StructurePanelV) while CTRL+C is handled by the Devexpress` TreeListControl inside the StructurePanelView.
Because the TLC handles the PreviewKeyDown- but the KeyBinding listens on the KeyDown-event CTRL+C doesn't work (for the order of events please have a look at this article https://wpf.2000things.com/2012/08/07/619-event-sequence-for-the-key-updown-events/).
It seems there is no possibility to define a XAML-KeyBinding as a PreviewKeybinding.
The link provided by Laganimal works fine becaue it also registers on the PreviewKeyDown-event.
I am only beginning to learn MVVM and its use in WPF.
I am using it to build a very simple Calculator application (like the Windows built in calculator).
One thing i've noticed, is that binding my view (XAML) to commands does not let me configure WHEN these actual commands are fired.
For example, a Button control fires the command bound to it when it is clicked.
I would like to achieve the same effect when the numpad buttons are presed ('1' will fire the Command of the "1" Button control, and so on).
I could not find 1 way to do this, all sites seem to show multiple other options which don't seem valid in this case.
Should this be set up in the Control's (Button) level or on the entire window? How can i do this ?
Try to set input bindings of your main window.
<Window.InputBindings>
<KeyBinding Key="D1" Command="{Binding Command1}" />
<KeyBinding Key="NumPad1" Command="{Binding Command1}" />
</Window.InputBindings>
I want to set two shortkeys for two buttons with one shortkey name.
How can i detect which control is focused in MVVM ?
You can simply add the hotkey to whatever scope is logical for it to exist in. There are many ways to handle hotkeys to WPF, however your basic markup should look something like this:
<Window>
<StackPanel>
<local:MyUserControlA>
<local:MyUserControlA.InputBindings>
<KeyBinding Key="Enter" Command="{Binding SaveACommand}" />
</local:MyUserControlA.InputBindings>
</local:MyUserControlA>
<local:MyUserControlB>
<local:MyUserControlB.InputBindings>
<KeyBinding Key="Enter" Command="{Binding SaveBCommand}" />
</local:MyUserControlB.InputBindings>
</local:MyUserControlB>
</StackPanel>
</Window>
This will run SaveACommand if UserControlA has keyboard focus, or SaveBCommand if UserControlB has keyboard focus.
Does anybody know whether one can trigger prism command with a shortcut? What I mean is I want to be able to define binding of a command to keyboard shortcut in declarative manner, like ClientUI does:
Are there any opensource libraries for that purpose? Or maybe code samples?
I found this question but I don't think that it answers mine.
I've created such gesture trigger. And I'd like to share it with you guys. Basically, it is System.Windows.Interactivity trigger which can parse gestures represented as strings. Usage is as simple as in ClientUI:
<UserControl>
<i:Interaction.Triggers>
<behaviors:KeystrokeCommandTrigger Command="{Binding SaveChangesCommand}" Gesture="Ctrl+Shift+S" />
<behaviors:KeystrokeCommandTrigger Command="{Binding RejectChangesCommand}" Gesture="Ctrl+Shift+R" />
<behaviors:KeystrokeCommandTrigger Command="{Binding NewItemCommand}" Gesture="Ins" />
<behaviors:KeystrokeCommandTrigger Command="{Binding DeleteSelectedItemCommand}" Gesture="Del" />
<behaviors:KeystrokeCommandTrigger Command="{Binding UploadSomethingCommand}" Gesture="Ctrl+Shift+U" />
</i:Interaction.Triggers>
</UserControl>
The code is on pastie.
You could write an attached behavior that has a listens to the KeyUp event and then calls the Command. The complication comes in translating something like Gesture="Ctrl+Shift+A". You would need to write a parser to figure out exactly what key combination that string represents.