ExInvokeCommandAction.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Windows.Input;
  7. using System.Windows;
  8. namespace HandyControl.Interactivity
  9. {
  10. public class ExCommandParameter
  11. {
  12. /// <summary>
  13. /// 事件触发源
  14. /// </summary>
  15. [AllowNull]
  16. public DependencyObject Sender { get; set; }
  17. /// <summary>
  18. /// 事件参数
  19. /// </summary>
  20. [AllowNull]
  21. public EventArgs EventArgs { get; set; }
  22. /// <summary>
  23. /// 额外参数
  24. /// </summary>
  25. [AllowNull]
  26. public object Parameter { get; set; }
  27. [AllowNull]
  28. public object CommandName { get; set; }
  29. }
  30. public class ExInvokeCommandAction : TriggerAction<DependencyObject>
  31. {
  32. /* public bool IsEnabled
  33. {
  34. get { return (bool)GetValue(IsEnabledProperty); }
  35. set { SetValue(IsEnabledProperty, value); }
  36. }
  37. // Using a DependencyProperty as the backing store for IsEnabled. This enables animation, styling, binding, etc...
  38. public static readonly DependencyProperty IsEnabledProperty =
  39. DependencyProperty.Register("IsEnabled", typeof(bool), typeof(ExInvokeCommandAction), new PropertyMetadata(true));
  40. */
  41. public static readonly DependencyProperty CommandNameProperty = DependencyProperty.Register("CommandName", typeof(object), typeof(ExInvokeCommandAction), null);
  42. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExInvokeCommandAction), null);
  43. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExInvokeCommandAction), null);
  44. /// <summary>
  45. /// 获得或设置此操作应调用的命令的名称。
  46. /// </summary>
  47. /// <value>此操作应调用的命令的名称。</value>
  48. /// <remarks>如果设置了此属性和 Command 属性,则此属性将被后者所取代。</remarks>
  49. public object CommandName
  50. {
  51. get
  52. {
  53. base.ReadPreamble();
  54. return GetValue(CommandNameProperty);
  55. }
  56. set
  57. {
  58. if (this.CommandName != value)
  59. {
  60. base.WritePreamble();
  61. SetValue(CommandNameProperty, value);
  62. base.WritePostscript();
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// 获取或设置此操作应调用的命令。这是依赖属性。
  68. /// </summary>
  69. /// <value>要执行的命令。</value>
  70. /// <remarks>如果设置了此属性和 CommandName 属性,则此属性将优先于后者。</remarks>
  71. public ICommand Command
  72. {
  73. get
  74. {
  75. return (ICommand)GetValue(CommandProperty);
  76. }
  77. set
  78. {
  79. SetValue(CommandProperty, value);
  80. }
  81. }
  82. /// <summary>
  83. /// 获得或设置命令参数。这是依赖属性。
  84. /// </summary>
  85. /// <value>命令参数。</value>
  86. /// <remarks>这是传递给 ICommand.CanExecute 和 ICommand.Execute 的值。</remarks>
  87. public object CommandParameter
  88. {
  89. get
  90. {
  91. return GetValue(CommandParameterProperty);
  92. }
  93. set
  94. {
  95. SetValue(CommandParameterProperty, value);
  96. }
  97. }
  98. /// <summary>
  99. /// 调用操作。
  100. /// </summary>
  101. /// <param name="parameter">操作的参数。如果操作不需要参数,则可以将参数设置为空引用。</param>
  102. protected override void Invoke(object parameter)
  103. {
  104. if (base.AssociatedObject != null)
  105. {
  106. ICommand? command = this.ResolveCommand();
  107. if (command == null) return;
  108. /*
  109. * ★★★★★★★★★★★★★★★★★★★★★★★★
  110. * 注意这里添加了事件触发源和事件参数
  111. * ★★★★★★★★★★★★★★★★★★★★★★★★
  112. */
  113. ExCommandParameter exParameter = new ExCommandParameter
  114. {
  115. Sender = base.AssociatedObject,
  116. Parameter = GetValue(CommandParameterProperty),
  117. EventArgs = parameter as EventArgs,
  118. CommandName = GetValue(CommandNameProperty)
  119. };
  120. if (command != null && command.CanExecute(exParameter))
  121. {
  122. /*
  123. * ★★★★★★★★★★★★★★★★★★★★★★★★
  124. * 注意将扩展的参数传递到Execute方法中
  125. * ★★★★★★★★★★★★★★★★★★★★★★★★
  126. */
  127. command.Execute(exParameter);
  128. }
  129. }
  130. }
  131. private ICommand? ResolveCommand()
  132. {
  133. ICommand? result = null;
  134. if (this.Command != null)
  135. {
  136. result = this.Command;
  137. }
  138. else
  139. {
  140. if (base.AssociatedObject != null)
  141. {
  142. Type type = base.AssociatedObject.GetType();
  143. PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
  144. PropertyInfo[] array = properties;
  145. for (int i = 0; i < array.Length; i++)
  146. {
  147. PropertyInfo propertyInfo = array[i];
  148. if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType) && string.Equals(propertyInfo.Name, this.CommandName?.ToString(), StringComparison.Ordinal))
  149. {
  150. result = (ICommand?)propertyInfo.GetValue(base.AssociatedObject, null);
  151. }
  152. }
  153. }
  154. }
  155. return result;
  156. }
  157. }
  158. }