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>
Related
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.
This seems like such a basic question but after hours of searching around and not figuring out what I'm doing wrong I decided it's time to ask for help!
I'm new to WPF and the MVVM pattern, but am trying to create an application that has several windows you can navigate through by clicking buttons. This is accomplished by having the app window display UserControls using DataTemplates, so there's no content currently shared between pages (though there will be once I create the navigation area). Here's what the XAML looks like for the main window, with there currently only being one page in the application:
<Window x:Class="WPF_Application.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_Application"
Title="ApplicationView" Height="300" Width="300" WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Window.Resources>
<DataTemplate DataType="{x:Type local:LoginMenuViewModel}">
<local:LoginMenuView />
</DataTemplate>
</Window.Resources>
<DockPanel>
<ContentControl
Content="{Binding CurrentPageViewModel}" />
</DockPanel>
Now what I'd like to do is add a KeyBinding that reacts to the escape button being pressed. When this is done "LogoutCommand" should fire in the LoginMenuViewModel. I'm stuck getting the keybinding to trigger any commands within LoginMenuViewModel, and I've figured it's probably because the DataContext needs to be set to reference LoginMenuViewModel. For the life of me I can't get this to work.
Am I going about application-wide commands completely the wrong way? Is there some super simple fix that will make me smack my forehead in shame? Any insight is appreciated!
I do not know your ViewModel code, so it is not easy to give you details hints.
First of all, if your are using MVVM, you should have your own implementation of ICommand interface. You can find here the most common one (and a simple MVVM tutorial too).
After you have your own command class, your ViewModel should expose your LogoutCommand:
public class ViewModel
{
/* ... */
public ICommand LogoutCommand
{
get
{
return /* your command */
}
}
/* ... */
}
In your code behind you will set: DataContext = new ViewModel(); and at this point you can declare the KeyBindings that you need:
<Window.InputBindings>
<KeyBinding Key="Escape" Command="{Binding Path=LogoutCommand, Mode=OneWay}" />
</Window.InputBindings>
In this way when the Window is active and the user press the "Esc" key, your LogoutCommand is executed.
This is a brief summary that I hope will guide you in deepening the MVVM and its command system.
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.
I have an application where I have a UserControl with a toolbar in it. This toolbar has a button Execute which in turn has its command bound to an ICommand derived class that the viewmodel exposes as a property.
<Button Grid.Row="0" Command="{Binding ExecuteCommand}">Execute</Button>
Now, I'd like to bind this to a keyboard shortcut (F5) as well. This needs to be bound in the context of the UserControl since, it's only applicable if this usercontrol is visible at the moment.
Another option is to bind it to the KeyDown of the textbox that actually contains the text to execute, but I'm really shaky when it comes to how to route the event from the control to the command in the viewmodel without really ugly hacks in the code-behind of the usercontrol.
Any pointers are appreciated!
There was another answer that disappeared for some reason. This worked fine:
<UserControl.InputBindings>
<KeyBinding Key="F5" Command="{Binding ExecuteCommand}" />
</UserControl.InputBindings>
I'd like to give credit to that guy if possible. Please appear again :)
Afaik, there isn't a way to directly bind to a keypress, but there are some work arounds. It looks like others have had this problem as well, have you seen this post? My other suggestion is to look into attached commands.
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>