RelayCommand.cs 919 B

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