RelayCommand.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 EfemDualUI.Controls.Common
  8. {
  9. public class RelayCommand : ICommand
  10. {
  11. private readonly Func<Object, Boolean> canExecute;
  12. private readonly Action<Object> execute;
  13. public RelayCommand(Action<Object> execute) : this(execute, null)
  14. {
  15. }
  16. public RelayCommand(Action<Object> execute, Func<Object, Boolean> canExecute)
  17. {
  18. this.execute = execute ?? throw new ArgumentNullException("execute");
  19. this.canExecute = canExecute;
  20. }
  21. public event EventHandler CanExecuteChanged
  22. {
  23. add
  24. {
  25. if (canExecute != null)
  26. CommandManager.RequerySuggested += value;
  27. }
  28. remove
  29. {
  30. if (canExecute != null)
  31. CommandManager.RequerySuggested -= value;
  32. }
  33. }
  34. public Boolean CanExecute(Object parameter)
  35. {
  36. return canExecute == null ? true : canExecute(parameter);
  37. }
  38. public void Execute(Object parameter)
  39. {
  40. execute(parameter);
  41. }
  42. }
  43. }