multi key gesture in wpf

前端 未结 4 1623
独厮守ぢ
独厮守ぢ 2020-12-21 05:55

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

4条回答
  •  星月不相逢
    2020-12-21 06:21

    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!

提交回复
热议问题