I have a RoutedUICommand called Comment Selection. I need to add an input gesture for this command as it is in VIsual Studio, ie. (Ctrl+K
Here's how I cobbled together something that actually works. I just wish I could credit the person or persons who paved the way to my Path of Enlightenment.
Let's say your application is called Heckler. Add a namespace tag for your application to the Window object:
Now add a CommandBindings property tag and start your collection of CommandBinding objects. Here we add custom command Comment Selection:
Add a MenuItem to a main Menu's MenuItem:
...
In the Window code-behind, add your CustomCommands class and custom command:
public static class CustomCommands
{
// Ctrl+Shift+C to avoid collision with Ctrl+C.
public static readonly RoutedUICommand CommentSelection =
new RoutedUICommand("_Comment Selection",
"CommentSelection", typeof(MainWindow),
new InputGestureCollection()
{ new KeyGesture(Key.C, (ModifierKeys.Control | ModifierKeys.Shift)) });
}
Now wire up your event handlers:
private void CommentSelectionCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
// Determines status of command.
e.CanExecute = true;
}
private void CommentSelectionExecuted(object sender, ExecutedRoutedEventArgs e)
{
// TO-DO: Insert magic here.
}
You should be good to go. I hope this helps and I didn't miss anything!