RelayCommand.cs 945 B

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