123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Reflection;
- using System.Text;
- using System.Windows.Input;
- using System.Windows;
- namespace HandyControl.Interactivity
- {
- public class ExCommandParameter
- {
- /// <summary>
- /// 事件触发源
- /// </summary>
- [AllowNull]
- public DependencyObject Sender { get; set; }
- /// <summary>
- /// 事件参数
- /// </summary>
- [AllowNull]
- public EventArgs EventArgs { get; set; }
- /// <summary>
- /// 额外参数
- /// </summary>
- [AllowNull]
- public object Parameter { get; set; }
- [AllowNull]
- public object CommandName { get; set; }
- }
- public class ExInvokeCommandAction : TriggerAction<DependencyObject>
- {
- public static readonly DependencyProperty CommandNameProperty = DependencyProperty.Register("CommandName", typeof(object), typeof(ExInvokeCommandAction), null);
- public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExInvokeCommandAction), null);
- public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExInvokeCommandAction), null);
- /// <summary>
- /// 获得或设置此操作应调用的命令的名称。
- /// </summary>
- /// <value>此操作应调用的命令的名称。</value>
- /// <remarks>如果设置了此属性和 Command 属性,则此属性将被后者所取代。</remarks>
- public object CommandName
- {
- get
- {
- base.ReadPreamble();
- return GetValue(CommandNameProperty);
- }
- set
- {
- if (this.CommandName != value)
- {
- base.WritePreamble();
- SetValue(CommandNameProperty, value);
- base.WritePostscript();
- }
- }
- }
- /// <summary>
- /// 获取或设置此操作应调用的命令。这是依赖属性。
- /// </summary>
- /// <value>要执行的命令。</value>
- /// <remarks>如果设置了此属性和 CommandName 属性,则此属性将优先于后者。</remarks>
- public ICommand Command
- {
- get
- {
- return (ICommand)GetValue(CommandProperty);
- }
- set
- {
- SetValue(CommandProperty, value);
- }
- }
- /// <summary>
- /// 获得或设置命令参数。这是依赖属性。
- /// </summary>
- /// <value>命令参数。</value>
- /// <remarks>这是传递给 ICommand.CanExecute 和 ICommand.Execute 的值。</remarks>
- public object CommandParameter
- {
- get
- {
- return GetValue(CommandParameterProperty);
- }
- set
- {
- SetValue(CommandParameterProperty, value);
- }
- }
- /// <summary>
- /// 调用操作。
- /// </summary>
- /// <param name="parameter">操作的参数。如果操作不需要参数,则可以将参数设置为空引用。</param>
- protected override void Invoke(object parameter)
- {
- if (base.AssociatedObject != null)
- {
- ICommand? command = this.ResolveCommand();
- if (command == null) return;
- /*
- * ★★★★★★★★★★★★★★★★★★★★★★★★
- * 注意这里添加了事件触发源和事件参数
- * ★★★★★★★★★★★★★★★★★★★★★★★★
- */
- ExCommandParameter exParameter = new ExCommandParameter
- {
- Sender = base.AssociatedObject,
- Parameter = GetValue(CommandParameterProperty),
- EventArgs = parameter as EventArgs,
- CommandName = GetValue(CommandNameProperty)
- };
- if (command != null && command.CanExecute(exParameter))
- {
- /*
- * ★★★★★★★★★★★★★★★★★★★★★★★★
- * 注意将扩展的参数传递到Execute方法中
- * ★★★★★★★★★★★★★★★★★★★★★★★★
- */
- command.Execute(exParameter);
- }
- }
- }
- private ICommand? ResolveCommand()
- {
- ICommand? result = null;
- if (this.Command != null)
- {
- result = this.Command;
- }
- else
- {
- if (base.AssociatedObject != null)
- {
- Type type = base.AssociatedObject.GetType();
- PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
- PropertyInfo[] array = properties;
- for (int i = 0; i < array.Length; i++)
- {
- PropertyInfo propertyInfo = array[i];
- if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType) && string.Equals(propertyInfo.Name, this.CommandName?.ToString(), StringComparison.Ordinal))
- {
- result = (ICommand?)propertyInfo.GetValue(base.AssociatedObject, null);
- }
- }
- }
- }
- return result;
- }
- }
- }
|