AnotherCommandImplementation.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Windows.Input;
  3. namespace WatchDog.WPF
  4. {
  5. public class AnotherCommandImplementation : ICommand
  6. {
  7. private readonly Action<object> _execute;
  8. private readonly Func<object, bool> _canExecute;
  9. public AnotherCommandImplementation(Action<object> execute) : this(execute, null)
  10. {
  11. }
  12. public AnotherCommandImplementation(Action<object> execute, Func<object, bool> canExecute)
  13. {
  14. if (execute == null) throw new ArgumentNullException(nameof(execute));
  15. _execute = execute;
  16. _canExecute = canExecute ?? (x => true);
  17. }
  18. public bool CanExecute(object parameter)
  19. {
  20. return _canExecute(parameter);
  21. }
  22. public void Execute(object parameter)
  23. {
  24. _execute(parameter);
  25. }
  26. public event EventHandler CanExecuteChanged
  27. {
  28. add
  29. {
  30. CommandManager.RequerySuggested += value;
  31. }
  32. remove
  33. {
  34. CommandManager.RequerySuggested -= value;
  35. }
  36. }
  37. public void Refresh()
  38. {
  39. CommandManager.InvalidateRequerySuggested();
  40. }
  41. }
  42. }