Are there any components or elements that allow you to configure keyboard shortcuts to click events of elements within WPF windows?
Thank you.
Are there any components or elements that allow you to configure keyboard shortcuts to click events of elements within WPF windows?
Thank you.
Hi Diego,
I suggest you to use Google Translator to get my answer in Spanish, as I did to understand your question and translating it in my language.
Here is some hints for your question, it's really succint so if you need more feel free to ask:
//This is the class where you define a Command
//In My case it is set in a folder named Commands inside the WPF project
namespace MyCompany.MyApp.Commands
{
public static class MyCommandsClass
{
public static readonly RoutedCommand OpenConfig = new RoutedCommand();
}
}
//This has to be set in the XAML part of the Window
//In example: MyWindow.xaml
<Window
x:Class="MYCompany.MyApp.Windows.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lcmd="clr-namespace:MYCompany.MyApp.Commands"
... More namespaces and class properties for Window
... >
<Window.Resources>
.... here you can have styles and other resources
</Window.Resources>
<!-- The following section of the window defines the event handlers for the command to be executed
CanExecute and Executed are the command events and the 2 event handlers have to be set in your code
to set if the command can execute and to implement the command code. -->
<Window.CommandBindings>
<CommandBinding
CanExecute="CommandToExecute_CanExecute"
Command="{x:Static lcmd:MyCommandsClass.CommandToExecute}"
Executed="CommandToExecute_Executed" />
</Window.CommandBindings>
<!-- The following section of the window assigns to the command a combination of keys used to execute the command.
in my case, Alt+A executes the code put into rhe CommandToExecute_Executed event handler -->
<Window.InputBindings>
<KeyBinding
Command="{x:Static lcmd:MyCommandsClass.CommandToExecute}"
CommandParameter="ParameterValue"
Gesture="Alt+A" />
<Window.InputBindings>
//This part instead has to be written in the .xaml.cs part of the window
// in example MyWindow.xaml.cs
//Here is the event that WPF uses to activate the execution of the command
//This is particularly useful to activate or deactivate buttons
//In my case I check that the ViewModel of the window has been instantiated and that
//a property has been instantiated
private void CommandToExecute_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = WModel != null && WModel.MyModelProperty != null;
}
//Here is the event handler for the code executed when Alt+A is pressed
//In this case I Get the CommandParameter value and pass it to the Method
//That executes my code.
private void CommandToExecute_Executed(object sender, ExecutedRoutedEventArgs e)
{
try
{
string fieldName = e.Parameter.ToString();
WModel.MyCommandFunction(this, fieldName);
}
catch (Exception ex)
{
EventLogger.SendMsg(ex);
Msg.Error(ex);
}
}
HTH bye Sabrina
hi, maybe make this
create keydown event in window
and write this
ex.
if(e.Key==Key.LeftCtrl && Keyborad.IsKeyDown(Key.A))
{
MessageBox.Show("Ctrl+a Pressed");
}
27 people are following this question.