WPF Bind button to key pressed - c#

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>

Related

WPF handle shortcut inside TabControl (CTRL+F search)

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>

Bind Buttons to ScrollWheel and Keyboard

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>

Set KeyBinding on a whole WPF window

I working on an MVVM window and want to control something in the view model by the keyboard, but if i place the following code directly under the window it can't be compiled only if i place under for example a text box. How can i do this?
<KeyBinding Key="P" Command="{Binding ToggleCommand}"/>
You need to assign the KeyBinding to the InputBindings property on Window
<Window.InputBindings>
<KeyBinding Key="P" Command="{Binding ToggleCommand}"/>
</Window.InputBindings>

Handling keyboard press in WPF

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>

Detect which Control is Focused?

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.

Categories

Resources