RelayCommand.cs 841 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Windows.Input;
  3. namespace MVVM
  4. {
  5. public class RelayCommand : ICommand
  6. {
  7. private Action<object> execute;
  8. private Func<object, bool> canExecute;
  9. public event EventHandler CanExecuteChanged
  10. {
  11. add { CommandManager.RequerySuggested += value; }
  12. remove { CommandManager.RequerySuggested -= value; }
  13. }
  14. public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
  15. {
  16. this.execute = execute;
  17. this.canExecute = canExecute;
  18. }
  19. public bool CanExecute(object parameter)
  20. {
  21. return this.canExecute == null || this.canExecute(parameter);
  22. }
  23. public void Execute(object parameter)
  24. {
  25. this.execute(parameter);
  26. }
  27. }
  28. }