I am writing a WPF application with just a few commands, so I am using a toolbar, without a menu. I would like to assign ctrl-key shortcuts to the buttons on the toolbar. Is there a simple way to do this, without having to create routed commands or ICommands just to support the shortcuts? I would prefer to do it in XAML, rather than code-behind.
A:
You do need code, one event tells the command if it can be executed (no more than a few lines usually), the other what to do, so every control bound to it does the same thing. Here's a very simple example:
XAML:
<UserControl x:Class="WPFTests.AppCommands"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<UserControl.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</UserControl.CommandBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" Height="16" Name="checkBoxCanExecute" Margin="8" IsChecked="true">Can execute</CheckBox>
<Button Grid.Row="1" Height="24" Padding="8,0,8,0" HorizontalAlignment="Left" Margin="8" Command="ApplicationCommands.New">ApplicationCommand.New</Button>
</Grid>
</UserControl>
C#
using System.Windows; using System.Windows.Controls; using System.Windows.Input;
namespace WPFTests
{
/// <summary>
/// Interaction logic for AppCommands.xaml
/// </summary>
public partial class AppCommands : UserControl
{
public AppCommands()
{
InitializeComponent();
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = (bool)checkBoxCanExecute.IsChecked;
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("New executed");
}
}
}
Carlo
2009-09-10 19:47:16
Thanks for the response, but I don't think it answers the question that I asked. All I want to do is put a Ctrl-Key on the button; for example, Ctrl-O to to invoke the 'Open File' button.
David Veeneman
2009-09-11 21:35:27
<Button Command="ApplicationCommands.New"> This puts the keyboard gesture Ctrl + N on the button. If you were to do <Button Command="ApplicationCommands.Open"> it would put Ctrl + O. This is implicitly implemented in WPF's CommandBindings. You can even create your own command with your own gesture. And if you bind the command to a menu item, it will automatically show the gesture beside the command name without any extra effor, just <MenuItem Command="ApplicationCommands.New" />. This would show [New Ctrl + N] in the menu item.
Carlo
2009-09-11 22:19:30
A:
I have come to the conclusion that WPF doesn't provide a simple way to add a Ctrl-key to a button. I solved my problem by trapping keypresses in the code-behind, and then invoking the appropriate ICommand--the ICommand that the corresponding button's Command property is bound to. It's a bit of an ugly hack, but it works, and it seems to be the best approach under the circumstances.
David Veeneman
2009-09-11 21:38:33
Based on your comment on my answer, I'm not sure I understood your question. But I think your overkilling it. WPF CommandBindings do exactly what you need as long as the control you're using has a Command attribute. You just set the ApplicationCommand and they keyboard gesture is automatically set to it. In my example, if you do Ctrl + O, CommandBinding_Executed will get triggered.
Carlo
2009-09-11 22:22:26