Defining MenuItem Shortcuts

后端 未结 6 623
北恋
北恋 2020-11-27 14:02

I need a simple way to set a shortcut for menu items.

But this don´t work with shortcut, just with click:


    <         


        
6条回答
  •  半阙折子戏
    2020-11-27 14:53

    You need to use KeyBindings (and CommandBindings if you (re)use RoutedCommands such as those found in the ApplicationCommands class) for that in the controls where the shortcuts should work.

    e.g.

    
            
    
    
            
    
    

    For custom RoutedCommands:

    static class CustomCommands
    {
        public static RoutedCommand DoStuff = new RoutedCommand();
    }
    

    usage:

    
            
                    
            
            
                    
            
        ...
    
    

    (It is often more convenient to implement the ICommand interface rather than using RoutedCommands. You can have a constructor which takes delegates for Execute and CanExecute to easily create commands which do different things, such implementations are often called DelegateCommand or RelayCommand. This way you do not need CommandBindings.)

提交回复
热议问题