123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using Microsoft.Xaml.Behaviors;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Data;
- using System.Windows.Input;
- using System.Windows;
- namespace PDF_Office.Helper
- {
- public class CompositeCommandParameter
- {
- public CompositeCommandParameter(EventArgs eventArgs, object parameter)
- {
- EventArgs = eventArgs;
- Parameter = parameter;
- }
- public EventArgs EventArgs { get; }
- public object Parameter { get; }
- }
- public sealed class AdvancedInvokeCommandAction : TriggerAction<DependencyObject>
- {
- private string commandName;
- public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(AdvancedInvokeCommandAction), null);
- public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(AdvancedInvokeCommandAction), null);
- public static readonly DependencyProperty EventArgsConverterProperty = DependencyProperty.Register("EventArgsConverter", typeof(IValueConverter), typeof(AdvancedInvokeCommandAction), new PropertyMetadata(null));
- public static readonly DependencyProperty EventArgsConverterParameterProperty = DependencyProperty.Register("EventArgsConverterParameter", typeof(object), typeof(AdvancedInvokeCommandAction), new PropertyMetadata(null));
- public static readonly DependencyProperty EventArgsParameterPathProperty = DependencyProperty.Register("EventArgsParameterPath", typeof(string), typeof(AdvancedInvokeCommandAction), new PropertyMetadata(null));
-
-
-
-
-
- public string CommandName
- {
- get
- {
- this.ReadPreamble();
- return this.commandName;
- }
- set
- {
- if (this.CommandName != value)
- {
- this.WritePreamble();
- this.commandName = value;
- this.WritePostscript();
- }
- }
- }
-
-
-
-
-
- public ICommand Command
- {
- get { return (ICommand)this.GetValue(CommandProperty); }
- set { this.SetValue(CommandProperty, value); }
- }
-
-
-
-
-
- public object CommandParameter
- {
- get { return this.GetValue(AdvancedInvokeCommandAction.CommandParameterProperty); }
- set { this.SetValue(AdvancedInvokeCommandAction.CommandParameterProperty, value); }
- }
-
-
-
-
- public IValueConverter EventArgsConverter
- {
- get { return (IValueConverter)GetValue(EventArgsConverterProperty); }
- set { SetValue(EventArgsConverterProperty, value); }
- }
-
-
-
- public object EventArgsConverterParameter
- {
- get { return (object)GetValue(EventArgsConverterParameterProperty); }
- set { SetValue(EventArgsConverterParameterProperty, value); }
- }
-
-
-
-
- public string EventArgsParameterPath
- {
- get { return (string)GetValue(EventArgsParameterPathProperty); }
- set { SetValue(EventArgsParameterPathProperty, value); }
- }
-
-
-
-
- public bool PassEventArgsToCommand { get; set; }
-
-
-
-
- protected override void Invoke(object parameter)
- {
- if (this.AssociatedObject != null)
- {
- ICommand command = this.ResolveCommand();
- if (command != null)
- {
- object eventArgs = null;
- object commandParameter = this.CommandParameter;
-
- if (!string.IsNullOrWhiteSpace(this.EventArgsParameterPath))
- {
- eventArgs = GetEventArgsPropertyPathValue(parameter);
- }
-
- if (eventArgs == null && this.EventArgsConverter != null)
- {
- eventArgs = this.EventArgsConverter.Convert(parameter, typeof(object), EventArgsConverterParameter, CultureInfo.CurrentCulture);
- }
-
- if (eventArgs == null && this.PassEventArgsToCommand)
- {
- eventArgs = parameter;
- }
- if (command.CanExecute(commandParameter))
- {
- var compositeCommandParameter = new CompositeCommandParameter((EventArgs)eventArgs, commandParameter);
- command.Execute(compositeCommandParameter);
- }
- }
- }
- }
- private object GetEventArgsPropertyPathValue(object parameter)
- {
- object commandParameter;
- object propertyValue = parameter;
- string[] propertyPathParts = EventArgsParameterPath.Split('.');
- foreach (string propertyPathPart in propertyPathParts)
- {
- PropertyInfo propInfo = propertyValue.GetType().GetProperty(propertyPathPart);
- propertyValue = propInfo.GetValue(propertyValue, null);
- }
- commandParameter = propertyValue;
- return commandParameter;
- }
- private ICommand ResolveCommand()
- {
- ICommand command = null;
- if (this.Command != null)
- {
- command = this.Command;
- }
- else if (this.AssociatedObject != null)
- {
-
- Type associatedObjectType = this.AssociatedObject.GetType();
- PropertyInfo[] typeProperties = associatedObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
- foreach (PropertyInfo propertyInfo in typeProperties)
- {
- if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType))
- {
- if (string.Equals(propertyInfo.Name, this.CommandName, StringComparison.Ordinal))
- {
- command = (ICommand)propertyInfo.GetValue(this.AssociatedObject, null);
- }
- }
- }
- }
- return command;
- }
- }
- }
|