ExInvokeCommandAction.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 static readonly DependencyProperty CommandNameProperty = DependencyProperty.Register("CommandName", typeof(object), typeof(ExInvokeCommandAction), null);
  33. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExInvokeCommandAction), null);
  34. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExInvokeCommandAction), null);
  35. /// <summary>
  36. /// 获得或设置此操作应调用的命令的名称。
  37. /// </summary>
  38. /// <value>此操作应调用的命令的名称。</value>
  39. /// <remarks>如果设置了此属性和 Command 属性,则此属性将被后者所取代。</remarks>
  40. public object CommandName
  41. {
  42. get
  43. {
  44. base.ReadPreamble();
  45. return GetValue(CommandNameProperty);
  46. }
  47. set
  48. {
  49. if (this.CommandName != value)
  50. {
  51. base.WritePreamble();
  52. SetValue(CommandNameProperty, value);
  53. base.WritePostscript();
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// 获取或设置此操作应调用的命令。这是依赖属性。
  59. /// </summary>
  60. /// <value>要执行的命令。</value>
  61. /// <remarks>如果设置了此属性和 CommandName 属性,则此属性将优先于后者。</remarks>
  62. public ICommand Command
  63. {
  64. get
  65. {
  66. return (ICommand)GetValue(CommandProperty);
  67. }
  68. set
  69. {
  70. SetValue(CommandProperty, value);
  71. }
  72. }
  73. /// <summary>
  74. /// 获得或设置命令参数。这是依赖属性。
  75. /// </summary>
  76. /// <value>命令参数。</value>
  77. /// <remarks>这是传递给 ICommand.CanExecute 和 ICommand.Execute 的值。</remarks>
  78. public object CommandParameter
  79. {
  80. get
  81. {
  82. return GetValue(CommandParameterProperty);
  83. }
  84. set
  85. {
  86. SetValue(CommandParameterProperty, value);
  87. }
  88. }
  89. /// <summary>
  90. /// 调用操作。
  91. /// </summary>
  92. /// <param name="parameter">操作的参数。如果操作不需要参数,则可以将参数设置为空引用。</param>
  93. protected override void Invoke(object parameter)
  94. {
  95. if (base.AssociatedObject != null)
  96. {
  97. ICommand? command = this.ResolveCommand();
  98. if (command == null) return;
  99. /*
  100. * ★★★★★★★★★★★★★★★★★★★★★★★★
  101. * 注意这里添加了事件触发源和事件参数
  102. * ★★★★★★★★★★★★★★★★★★★★★★★★
  103. */
  104. ExCommandParameter exParameter = new ExCommandParameter
  105. {
  106. Sender = base.AssociatedObject,
  107. Parameter = GetValue(CommandParameterProperty),
  108. EventArgs = parameter as EventArgs,
  109. CommandName = GetValue(CommandNameProperty)
  110. };
  111. if (command != null && command.CanExecute(exParameter))
  112. {
  113. /*
  114. * ★★★★★★★★★★★★★★★★★★★★★★★★
  115. * 注意将扩展的参数传递到Execute方法中
  116. * ★★★★★★★★★★★★★★★★★★★★★★★★
  117. */
  118. command.Execute(exParameter);
  119. }
  120. }
  121. }
  122. private ICommand? ResolveCommand()
  123. {
  124. ICommand? result = null;
  125. if (this.Command != null)
  126. {
  127. result = this.Command;
  128. }
  129. else
  130. {
  131. if (base.AssociatedObject != null)
  132. {
  133. Type type = base.AssociatedObject.GetType();
  134. PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
  135. PropertyInfo[] array = properties;
  136. for (int i = 0; i < array.Length; i++)
  137. {
  138. PropertyInfo propertyInfo = array[i];
  139. if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType) && string.Equals(propertyInfo.Name, this.CommandName?.ToString(), StringComparison.Ordinal))
  140. {
  141. result = (ICommand?)propertyInfo.GetValue(base.AssociatedObject, null);
  142. }
  143. }
  144. }
  145. }
  146. return result;
  147. }
  148. }
  149. }