RelayCommand.cs 856 B

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