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>
Related
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>
is there a way to define DataGrid.InputBindings for all DataGrids in my application? I have my own command therefore I place:
<DataGrid.InputBindings>
<KeyBinding Command="{Binding MyOwnCommand}" Key="Delete"/>
</DataGrid.InputBindings>
in every DataGrid... I've tried to set this as style without any success.
Thank you in advance!
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 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.