DelegateCommand.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Developed by doiTTeam => [email protected]
  2. using System;
  3. using System.Windows.Input;
  4. namespace DraggAnimatedPanelExample
  5. {
  6. /// <summary>
  7. /// An <see cref = "ICommand" /> whose delegates can be attached for <see cref = "Execute" /> and <see cref = "CanExecute" />.
  8. /// It also implements the <see cref = "IActiveAware" /> interface, which is useful when registering this command in a <see cref = "CompositeCommand" /> that monitors command's activity.
  9. /// </summary>
  10. /// <typeparam name = "T">Parameter type.</typeparam>
  11. /// <remarks>
  12. /// The constructor deliberately prevent the use of value types.
  13. /// Because ICommand takes an object, having a value type for T would cause unexpected behavior when CanExecute(null) is called during XAML initialization for command bindings.
  14. /// Using default(T) was considered and rejected as a solution because the implementor would not be able to distinguish between a valid and defaulted values.
  15. /// <para />
  16. /// Instead, callers should support a value type by using a nullable value type and checking the HasValue property before using the Value property.
  17. /// <example>
  18. /// <code>
  19. /// public MyClass()
  20. /// {
  21. /// this.submitCommand = new DelegateCommand&lt;int?&gt;(this.Submit, this.CanSubmit);
  22. /// }
  23. ///
  24. /// private bool CanSubmit(int? customerId)
  25. /// {
  26. /// return (customerId.HasValue &amp;&amp; customers.Contains(customerId.Value));
  27. /// }
  28. /// </code>
  29. /// </example>
  30. /// </remarks>
  31. public class DelegateCommand<T> : DelegateCommandBase
  32. {
  33. /// <summary>
  34. /// Initializes a new instance of <see cref = "DelegateCommand{T}" />.
  35. /// </summary>
  36. /// <param name = "executeMethod">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>
  37. /// <remarks>
  38. /// <seealso cref = "CanExecute" /> will always return true.
  39. /// </remarks>
  40. public DelegateCommand(Action<T> executeMethod)
  41. : this(executeMethod, (o) => true)
  42. {
  43. }
  44. /// <summary>
  45. /// Initializes a new instance of <see cref = "DelegateCommand{T}" />.
  46. /// </summary>
  47. /// <param name = "executeMethod">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>
  48. /// <param name = "canExecuteMethod">Delegate to execute when CanExecute is called on the command. This can be null.</param>
  49. /// <exception cref = "ArgumentNullException">When both <paramref name = "executeMethod" /> and <paramref name = "canExecuteMethod" /> ar <see langword = "null" />.</exception>
  50. public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
  51. : base((o) => executeMethod((T) o), (o) => canExecuteMethod((T) o))
  52. {
  53. if (executeMethod == null || canExecuteMethod == null)
  54. throw new ArgumentNullException("executeMethod");
  55. }
  56. ///<summary>
  57. /// Determines if the command can execute by invoked the <see cref = "Func{T,Bool}" /> provided during construction.
  58. ///</summary>
  59. ///<param name = "parameter">Data used by the command to determine if it can execute.</param>
  60. ///<returns>
  61. /// <see langword = "true" /> if this command can be executed; otherwise, <see langword = "false" />.
  62. ///</returns>
  63. public bool CanExecute(T parameter)
  64. {
  65. return base.CanExecute(parameter);
  66. }
  67. ///<summary>
  68. /// Executes the command and invokes the <see cref = "Action{T}" /> provided during construction.
  69. ///</summary>
  70. ///<param name = "parameter">Data used by the command.</param>
  71. public void Execute(T parameter)
  72. {
  73. base.Execute(parameter);
  74. }
  75. }
  76. /// <summary>
  77. /// An <see cref = "ICommand" /> whose delegates do not take any parameters for <see cref = "Execute" /> and <see cref = "CanExecute" />.
  78. /// </summary>
  79. /// <seealso cref = "DelegateCommandBase" />
  80. /// <seealso cref = "DelegateCommand{T}" />
  81. public class DelegateCommand : DelegateCommandBase
  82. {
  83. /// <summary>
  84. /// Creates a new instance of <see cref = "DelegateCommand" /> with the <see cref = "Action" /> to invoke on execution.
  85. /// </summary>
  86. /// <param name = "executeMethod">The <see cref = "Action" /> to invoke when <see cref = "ICommand.Execute" /> is called.</param>
  87. public DelegateCommand(Action executeMethod) : this(executeMethod, () => true)
  88. {
  89. }
  90. /// <summary>
  91. /// Creates a new instance of <see cref = "DelegateCommand" /> with the <see cref = "Action" /> to invoke on execution
  92. /// and a <see langword = "Func" /> to query for determining if the command can execute.
  93. /// </summary>
  94. /// <param name = "executeMethod">The <see cref = "Action" /> to invoke when <see cref = "ICommand.Execute" /> is called.</param>
  95. /// <param name = "canExecuteMethod">The <see cref = "Func{TResult}" /> to invoke when <see cref = "ICommand.CanExecute" /> is called</param>
  96. public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
  97. : base((o) => executeMethod(), (o) => canExecuteMethod())
  98. {
  99. if (executeMethod == null || canExecuteMethod == null)
  100. throw new ArgumentNullException("executeMethod");
  101. }
  102. ///<summary>
  103. /// Executes the command.
  104. ///</summary>
  105. public void Execute()
  106. {
  107. Execute(null);
  108. }
  109. /// <summary>
  110. /// Determines if the command can be executed.
  111. /// </summary>
  112. /// <returns>Returns <see langword = "true" /> if the command can execute,otherwise returns <see langword = "false" />.</returns>
  113. public bool CanExecute()
  114. {
  115. return CanExecute(null);
  116. }
  117. }
  118. }