handling window close button in wpf MVVM

后端 未结 2 763
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 03:56

is there a way to handle the window close button ie \"X\" in the top right corner in the viewmodel by binding to a command? or overriding the window.close command so that cl

2条回答
  •  孤街浪徒
    2020-12-09 04:09

    There are several methods for this. I have pointed out two methods below.

    1. You can use attached commands to bind the close button in your view model.

    2. You can use Below code

    Xaml:

    
        
            
                
            
        
        
        
    
    

    NOTE: Add System.Windows.Interactivity reference

    View Model

    private ICommand closeWindowCommand;
    
    public ICommand CloseWindowCommand
    {
          get
          {
              if (closeWindowCommand == null)
              {
                 closeWindowCommand = new RelayCommand(param => this.CloseWindow(), null);
              }
              return closeWindowCommand;
          }
     }
    
    private void CloseWindow()
    {
         //Do your operations
    }
    

    This is my RelayCommand class.

    public class RelayCommand : ICommand
    {
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The execute.
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }
    
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The execute.
        /// The can execute.
        public RelayCommand(Action execute, Predicate canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }
    
        /// 
        /// Defines the method that determines whether the command can execute in its current state.
        /// 
        /// Data used by the command.  If the command does not require data to be passed, this object can be set to null.
        /// 
        /// true if this command can be executed; otherwise, false.
        /// 
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }
    
        /// 
        /// Occurs when changes occur that affect whether or not the command should execute.
        /// 
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        /// 
        /// Defines the method to be called when the command is invoked.
        /// 
        /// Data used by the command.  If the command does not require data to be passed, this object can be set to null.
        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    
        /// 
        /// Action
        /// 
        private readonly Action _execute;
    
    
        /// 
        /// Predicate
        /// 
        private readonly Predicate _canExecute;
    }
    
        

    提交回复
    热议问题