CustomDelegateCommand.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Windows.Input;
  3. namespace SHJX.Service.Common.ElementHelper
  4. {
  5. /// <summary>
  6. /// 实现DelegateCommand
  7. /// </summary>
  8. class CustomDelegateCommand : ICommand
  9. {
  10. #region Property
  11. /// <summary>
  12. /// 命令所需执行的事件
  13. /// </summary>
  14. public Action<object> ExecuteCommand { get; set; }
  15. /// <summary>
  16. /// 命令是否可用所执行的事件
  17. /// </summary>
  18. public Func<object, bool> CanExecuteCommand { get; set; }
  19. #endregion
  20. public CustomDelegateCommand() { }
  21. public CustomDelegateCommand(Action<object> execute, Func<object, bool> canexecute)
  22. {
  23. ExecuteCommand = execute;
  24. CanExecuteCommand = canexecute;
  25. }
  26. /// <summary>
  27. /// 命令可用性获取
  28. /// </summary>
  29. /// <param name="parameter"></param>
  30. /// <returns></returns>
  31. public bool CanExecute(object parameter)
  32. {
  33. return CanExecuteCommand(parameter);
  34. }
  35. public event EventHandler CanExecuteChanged
  36. {
  37. add { CommandManager.RequerySuggested += value; }
  38. remove { CommandManager.RequerySuggested -= value; }
  39. }
  40. /// <summary>
  41. /// 命令具体执行
  42. /// </summary>
  43. /// <param name="parameter"></param>
  44. public void Execute(object parameter)
  45. {
  46. ExecuteCommand(parameter);
  47. }
  48. }
  49. }