RelayCommand.cs 948 B

1234567891011121314151617181920212223242526272829303132333435
  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 OUP
  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. }