using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Interactivity; using System.Reflection; using System.Linq; namespace Juick.Classes { public sealed class InvokeDelegateCommandAction : TriggerAction { /// /// /// public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(InvokeDelegateCommandAction), null); /// /// /// public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( "Command", typeof(ICommand), typeof(InvokeDelegateCommandAction), null); /// /// /// public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register( "InvokeParameter", typeof(object), typeof(InvokeDelegateCommandAction), null); private string commandName; /// /// /// public object InvokeParameter { get { return this.GetValue(InvokeParameterProperty); } set { this.SetValue(InvokeParameterProperty, value); } } /// /// /// public ICommand Command { get { return (ICommand)this.GetValue(CommandProperty); } set { this.SetValue(CommandProperty, value); } } /// /// /// public string CommandName { get { return this.commandName; } set { if (this.CommandName != value) { this.commandName = value; } } } /// /// /// public object CommandParameter { get { return this.GetValue(CommandParameterProperty); } set { this.SetValue(CommandParameterProperty, value); } } /// /// /// /// protected override void Invoke(object parameter) { this.InvokeParameter = parameter; if (this.AssociatedObject != null) { ICommand command = this.ResolveCommand(); if ((command != null) && command.CanExecute(this.CommandParameter)) { command.Execute(this.CommandParameter); } } } private ICommand ResolveCommand() { ICommand command = null; if (this.Command != null) { return this.Command; } var frameworkElement = this.AssociatedObject as FrameworkElement; if (frameworkElement != null) { object dataContext = frameworkElement.DataContext; if (dataContext != null) { PropertyInfo commandPropertyInfo = dataContext .GetType() .GetProperties(BindingFlags.Public | BindingFlags.Instance) .FirstOrDefault( p => typeof(ICommand).IsAssignableFrom(p.PropertyType) && string.Equals(p.Name, this.CommandName, StringComparison.Ordinal) ); if (commandPropertyInfo != null) { command = (ICommand)commandPropertyInfo.GetValue(dataContext, null); } } } return command; } } }