RelayCommand.cs 933 B

12345678910111213141516171819202122232425262728293031323334
  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 Rkis29.ViewModel
  8. {
  9. public class RelayCommand : ICommand
  10. {
  11. private Action<object> _execute;
  12. private Func<object, bool> _canExecute;
  13. public event EventHandler CanExecuteChanged
  14. {
  15. add { CommandManager.RequerySuggested += value; }
  16. remove { CommandManager.RequerySuggested -= value; }
  17. }
  18. public RelayCommand(Action<object> excute, Func<object, bool> canExecute = null)
  19. {
  20. _execute = excute;
  21. _canExecute = canExecute;
  22. }
  23. public bool CanExecute(object parameter)
  24. {
  25. return _canExecute == null || _canExecute(parameter);
  26. }
  27. public void Execute(object parameter)
  28. {
  29. _execute(parameter);
  30. }
  31. }
  32. }