DelegateCommand.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 bububu
  8. {
  9. class DelegateCommand : ICommand
  10. {
  11. Action<object> execute;
  12. Func<object, bool> canExecute;
  13. public event EventHandler CanExecuteChanged
  14. {
  15. add { CommandManager.RequerySuggested += value; }
  16. remove { CommandManager.RequerySuggested -= value; }
  17. }
  18. public bool CanExecute(object parameter)
  19. {
  20. if(canExecute != null)
  21. {
  22. return canExecute(parameter);
  23. }
  24. return true;
  25. }
  26. public void Execute(object parameter)
  27. {
  28. if (execute != null)
  29. {
  30. execute(parameter);
  31. }
  32. }
  33. public DelegateCommand(Action<object> executeAction) : this(executeAction, null)
  34. {
  35. }
  36. public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecuteFunc)
  37. {
  38. canExecute = canExecuteFunc;
  39. execute = executeAction;
  40. }
  41. }
  42. }