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>
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.
I need to handle Ctrl+F keys combo to invoke search function inside my TabControl.
OnKeyDown() called for each button pressed, but how I can handle combination of two buttons?
Just put this in your xaml :
<Window.InputBindings>
<KeyBinding Command="ApplicationCommands.YourCommandToHandle"
Gesture="CTRL+F" />
</Window.InputBindings>
In my application I have a Previous Button and a Next Button. When they are clicked, a particular function is called with different parameters.
I was wondering if there was a way that the Mouse ScrollWheel and the Left and Right keyboard keys could be bound to have the same result as clicking on these two Buttons?
I.E Pushing upwards on the ScrollWheel or Pressing the Right Key on the keyboard would be tantamount to clicking the Next Button and vice versa.
You can use Attached Behaviors. Create a behavior with command and parameter properties and bind. In the property changed just hook on needed events. Like here for MouseUp.
For the left and right key you could do it like this:
<Grid.InputBindings>
<KeyBinding Key="Left" Command="{Binding ExecutePreviousCommand}" />
<KeyBinding Key="Right" Command="{Binding ExecuteNextCommand}" />
</Grid.InputBindings>
I have not done WPF development before.
I am creating a calculator. I want the "one" button to be bound to the keyboard key "1".
So the click function of my button must execute when the keyboard key "1" is pressed.
Please advise on how to do this>
use this:
http://msdn.microsoft.com/en-us/library/system.windows.input.keygesture.aspx
<Window.InputBindings>
<KeyBinding Command="{Binding MyCommand}"
Gesture="CTRL+R" />
</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.