I'm writing my first WPF application and am trying to get my custom commands to work.
public static RoutedUICommand Header1 { get; private set; }
.
.
.
gestures = new InputGestureCollection();
gestures.Add(new KeyGesture(Key.D1, ModifierKeys.Control, "Ctrl+1"));
Header1 = new RoutedUICommand("Header 1", "Header1", typeof(EditCommands), gestures);
I then added a CommandBindings
section to my window's XAML.
<!-- local refers to my application's namespace -->
<Window.CommandBindings>
<CommandBinding Command="local:EditCommands.Header1" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"></CommandBinding>
</Window.CommandBindings>
And, finally, added a command entry to the associated Ribbon control.
<RibbonButton Label="Header 1" Command="local:EditCommands.Header1" SmallImageSource="Images\small.png" ToolTipTitle="Header 1" ToolTipDescription="" ToolTipImageSource="Images\small.png"></RibbonButton>
Clicking the Ribbon button executes the handler as expected. However, pressing Ctrl+1
seems to have no effect at all. How can I have my hot key recognized?
Something else must be going on. Could there be some element that handles the key input before it reaches the element with the command bindings that contain your command?
A useful tool for figuring out things like that is Snooop.
Using the code below HandleHeader1 is called whenever Ctrl+1 is pressed.
public static class MyCommands
{
public static RoutedUICommand Header1 { get; } = new RoutedUICommand("Header 1", "Header1", typeof(MyCommands), new InputGestureCollection { new KeyGesture(Key.D1, ModifierKeys.Control, "Ctrl+1") });
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void HandleHeader1(object sender, ExecutedRoutedEventArgs e)
{
}
}
<Window x:Class="WpfApplication2.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="local:MyCommands.Header1" Executed="HandleHeader1"/>
</Window.CommandBindings>
<StackPanel>
<TextBox/>
</StackPanel>
</Window>
Hi I completely agree with Johan and Jane, your code is worked for me. But I can reproduce that strange behavior only in one case. If you have several command with the same gesture only the first one that defined in Window.CommandBindings section will be triggered. Here is my code and you can check it out. 1. Xaml:
<ribbon:RibbonWindow x:Class="SoRibbonWpfApplivationHelpAttempt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
xmlns:soRibbonWpfApplivationHelpAttempt="clr-namespace:SoRibbonWpfApplivationHelpAttempt"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="soRibbonWpfApplivationHelpAttempt:EditCommands.PandaButton" Executed="CommandBinding_Executed_P" CanExecute="CommandBinding_CanExecute_P"></CommandBinding>
<CommandBinding Command="soRibbonWpfApplivationHelpAttempt:EditCommands.Header1" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"></CommandBinding>
</Window.CommandBindings>
<Grid>
<ribbon:Ribbon x:Name="RibbonWin" SelectedIndex="0" IsEnabled="True">
<ribbon:Ribbon.HelpPaneContent>
<ribbon:RibbonButton SmallImageSource="Images/Penguins.jpg"
ToolTipTitle="Header 1" ToolTipDescription="" ToolTipImageSource="Images/Penguins.jpg"
Command="soRibbonWpfApplivationHelpAttempt:EditCommands.Header1"/>
</ribbon:Ribbon.HelpPaneContent>
<ribbon:Ribbon.QuickAccessToolBar>
<ribbon:RibbonQuickAccessToolBar>
<ribbon:RibbonButton x:Name ="Save" SmallImageSource="Images\Koala.jpg"
ToolTipTitle="Header 1" ToolTipDescription="" ToolTipImageSource="Images/Koala.jpg"
Command="soRibbonWpfApplivationHelpAttempt:EditCommands.PandaButton"/>
</ribbon:RibbonQuickAccessToolBar>
</ribbon:Ribbon.QuickAccessToolBar>
</ribbon:Ribbon>
<TextBox VerticalAlignment="Bottom" HorizontalAlignment="Stretch" BorderBrush="Red"/>
</Grid>
2. Code behind:
public partial class MainWindow : RibbonWindow
{
public MainWindow()
{
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
Debug.WriteLine("header 1");
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_CanExecute_P(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_Executed_P(object sender, ExecutedRoutedEventArgs e)
{
Debug.WriteLine("panda");
}
}
public class EditCommands
{
public static RoutedUICommand Header1 { get; private set; }
public static RoutedUICommand PandaButton
{
get;
private set;
}
static EditCommands()
{
var gestures = new InputGestureCollection {new KeyGesture(Key.D1, ModifierKeys.Control, "Ctrl+1")};
Header1 = new RoutedUICommand("Header 1", "Header1", typeof(EditCommands), gestures);
var pandaG = new InputGestureCollection { new KeyGesture(Key.D2, ModifierKeys.Control, "Ctrl+2") };
PandaButton = new RoutedUICommand("Panda Button", "PandaButton", typeof(EditCommands), gestures);
}
}
So I can suggest you the only one thing; to check if there are some another commands (may be not custom) which are already using the gesture you try to define.
Regards,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With