RelayCommand.cs 860 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Windows.Input;
  3. namespace RKISPATTERN.Command
  4. {
  5. public class RelayCommand : ICommand
  6. {
  7. private Action<object> execute;
  8. private Func<object, bool> canExecute;
  9. public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
  10. {
  11. this.execute = execute;
  12. this.canExecute = canExecute;
  13. }
  14. public event EventHandler? CanExecuteChanged
  15. {
  16. add { CommandManager.RequerySuggested += value; }
  17. remove { CommandManager.RequerySuggested -= value; }
  18. }
  19. public bool CanExecute(object? parameter)
  20. {
  21. return this.canExecute == null || this.canExecute(parameter);
  22. }
  23. public void Execute(object? parameter)
  24. {
  25. this.execute(parameter);
  26. }
  27. }
  28. }