DelegateCommand.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #region (c) 2019 Gilles Macabies All right reserved
  2. // Author : Gilles Macabies
  3. // Solution : DataGridFilter
  4. // Projet : DataGridFilter
  5. // File : DelegateCommand.cs
  6. // Created : 05/11/2019
  7. #endregion
  8. using System;
  9. using System.Windows.Input;
  10. namespace DemoApplication.ModelView
  11. {
  12. /// <summary>
  13. /// DelegateCommand borrowed from
  14. /// http://www.wpftutorial.net/DelegateCommand.html
  15. /// </summary>
  16. public class DelegateCommand : ICommand
  17. {
  18. private readonly Predicate<object> _canExecute;
  19. private readonly Action<object> _execute;
  20. public DelegateCommand(Action<object> execute,
  21. Predicate<object> canExecute = null)
  22. {
  23. _execute = execute;
  24. _canExecute = canExecute;
  25. }
  26. public void RaiseCanExecuteChanged()
  27. {
  28. CanExecuteChanged?.Invoke(this, EventArgs.Empty);
  29. }
  30. #region ICommand Members
  31. public event EventHandler CanExecuteChanged;
  32. public bool CanExecute(object parameter)
  33. {
  34. return _canExecute == null || _canExecute(parameter);
  35. }
  36. public void Execute(object parameter)
  37. {
  38. _execute(parameter);
  39. }
  40. #endregion
  41. }
  42. }