RelayCommand.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 PixiEditor.Helpers
  8. {
  9. public class RelayCommand : ICommand
  10. {
  11. #region Fields
  12. readonly Action<object> _execute;
  13. readonly Predicate<object> _canExecute;
  14. #endregion // Fields
  15. #region Constructors
  16. public RelayCommand(Action<object> execute)
  17. : this(execute, null)
  18. {
  19. }
  20. public RelayCommand(Action<object> execute, Predicate<object> canExecute)
  21. {
  22. if (execute == null)
  23. throw new ArgumentNullException("execute");
  24. _execute = execute;
  25. _canExecute = canExecute;
  26. }
  27. #endregion // Constructors
  28. #region ICommand Members
  29. public bool CanExecute(object parameter)
  30. {
  31. return _canExecute == null ? true : _canExecute(parameter);
  32. }
  33. public event EventHandler CanExecuteChanged
  34. {
  35. add { CommandManager.RequerySuggested += value; }
  36. remove { CommandManager.RequerySuggested -= value; }
  37. }
  38. public void Execute(object parameter)
  39. {
  40. _execute(parameter);
  41. }
  42. #endregion // ICommand Members
  43. }
  44. }