1
0

DelegateCommandBase.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Developed by doiTTeam => [email protected]
  2. using System;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Windows.Input;
  5. namespace DraggAnimatedPanelExample
  6. {
  7. /// <summary>
  8. /// An <see cref = "ICommand" /> whose delegates can be attached for <see cref = "Execute" /> and <see cref = "CanExecute" />.
  9. /// </summary>
  10. public abstract class DelegateCommandBase : ICommand
  11. {
  12. private readonly Func<object, bool> canExecuteMethod;
  13. private readonly Action<object> executeMethod;
  14. /// <summary>
  15. /// Createse a new instance of a <see cref = "DelegateCommandBase" />, specifying both the execute action and the can execute function.
  16. /// </summary>
  17. /// <param name = "executeMethod">The <see cref = "Action" /> to execute when <see cref = "ICommand.Execute" /> is invoked.</param>
  18. /// <param name = "canExecuteMethod">The <see cref = "Func{Object,Bool}" /> to invoked when <see cref = "ICommand.CanExecute" /> is invoked.</param>
  19. protected DelegateCommandBase(Action<object> executeMethod, Func<object, bool> canExecuteMethod)
  20. {
  21. if (executeMethod == null || canExecuteMethod == null)
  22. throw new ArgumentNullException("executeMethod");
  23. this.executeMethod = executeMethod;
  24. this.canExecuteMethod = canExecuteMethod;
  25. }
  26. #region ICommand Members
  27. void ICommand.Execute(object parameter)
  28. {
  29. Execute(parameter);
  30. }
  31. bool ICommand.CanExecute(object parameter)
  32. {
  33. return CanExecute(parameter);
  34. }
  35. /// <summary>
  36. /// Occurs when changes occur that affect whether or not the command should execute.
  37. /// </summary>
  38. public event EventHandler CanExecuteChanged;
  39. #endregion
  40. /// <summary>
  41. /// Raises <see cref = "ICommand.CanExecuteChanged" /> on the UI thread so every
  42. /// command invoker can requery <see cref = "ICommand.CanExecute" /> to check if the
  43. /// <see cref = "CompositeCommand" /> can execute.
  44. /// </summary>
  45. protected virtual void OnCanExecuteChanged()
  46. {
  47. var handlers = CanExecuteChanged;
  48. if (handlers != null)
  49. {
  50. handlers(this, EventArgs.Empty);
  51. }
  52. }
  53. /// <summary>
  54. /// Raises <see cref = "DelegateCommandBase.CanExecuteChanged" /> on the UI thread so every command invoker
  55. /// can requery to check if the command can execute.
  56. /// <remarks>
  57. /// Note that this will trigger the execution of <see cref = "DelegateCommandBase.CanExecute" /> once for each invoker.
  58. /// </remarks>
  59. /// </summary>
  60. [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
  61. public void RaiseCanExecuteChanged()
  62. {
  63. OnCanExecuteChanged();
  64. }
  65. /// <summary>
  66. /// Executes the command with the provided parameter by invoking the <see cref = "Action{Object}" /> supplied during construction.
  67. /// </summary>
  68. /// <param name = "parameter"></param>
  69. protected void Execute(object parameter)
  70. {
  71. executeMethod(parameter);
  72. }
  73. /// <summary>
  74. /// Determines if the command can execute with the provided parameter by invoing the <see cref = "Func{Object,Bool}" /> supplied during construction.
  75. /// </summary>
  76. /// <param name = "parameter">The parameter to use when determining if this command can execute.</param>
  77. /// <returns>Returns <see langword = "true" /> if the command can execute. <see langword = "False" /> otherwise.</returns>
  78. protected bool CanExecute(object parameter)
  79. {
  80. return canExecuteMethod == null || canExecuteMethod(parameter);
  81. }
  82. }
  83. }