BaseCommand.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Windows.Input;
  3. namespace SAGA.DotNetUtils.WPF
  4. {
  5. public class BaseCommand : ICommand
  6. {
  7. private Func<object, bool> m_canExecute;
  8. private Action<object> m_execute;
  9. public event EventHandler CanExecuteChanged
  10. {
  11. add
  12. {
  13. System.Windows.Input.CommandManager.RequerySuggested += value;
  14. }
  15. remove
  16. {
  17. System.Windows.Input.CommandManager.RequerySuggested -= value;
  18. }
  19. }
  20. public BaseCommand(Action<object> execute, Func<object, bool> canExecute)
  21. {
  22. this.m_execute = execute;
  23. this.m_canExecute = canExecute;
  24. }
  25. public virtual bool CanExecute(object parameter)
  26. {
  27. if (this.m_canExecute != null)
  28. {
  29. return this.m_canExecute.Invoke(parameter);
  30. }
  31. return true;
  32. }
  33. public virtual void Execute(object parameter)
  34. {
  35. if (this.m_execute != null)
  36. {
  37. this.m_execute(parameter);
  38. }
  39. }
  40. }
  41. }