Binding a WPF ShortCut Key to a Command in the ViewModel

后端 未结 5 2208
我寻月下人不归
我寻月下人不归 2020-12-01 07:17

I have a WPF app that is using the MVVM pattern. Hooking up buttons to the VM is pretty straight forward since they implement the ICommand. I have a context menu that works

5条回答
  •  失恋的感觉
    2020-12-01 08:03

    Have been able to add Keybinding on the DataGrid level. Like this :

    Xaml :

    
                    
                        
                           ****
                            
                        
                    
                    **
                        **
                    
                    
                        
                    
    
    

    View Model :

    public ICommand DeleteCommand
                {
                    get
                    {
                        return new DelegateCommand(ExecuteCommand, CanExecute);
                    }
                }
    
      private void ExecuteCommand()
    {
    // your code to delete here.
       YourCollection.Remove(YourSelectedItem);
    }
    
    private void CanExecute()
    {
    // logic to check if the delete command can execute.
       return YourSelectedItem != null ;
    }
    

提交回复
热议问题