From 0623943dccad5c44831b2ad4a803eab2436e45e6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 1 Apr 2013 12:05:02 +0400 Subject: Using WPToolkit LongListSelector --- Juick/Classes/InvokeDelegateCommandAction.cs | 151 +++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 Juick/Classes/InvokeDelegateCommandAction.cs (limited to 'Juick/Classes/InvokeDelegateCommandAction.cs') diff --git a/Juick/Classes/InvokeDelegateCommandAction.cs b/Juick/Classes/InvokeDelegateCommandAction.cs new file mode 100644 index 0000000..24861cf --- /dev/null +++ b/Juick/Classes/InvokeDelegateCommandAction.cs @@ -0,0 +1,151 @@ +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; + } + } +} -- cgit v1.2.3