diff options
author | Vitaly Takmazov | 2013-04-01 12:05:02 +0400 |
---|---|---|
committer | Vitaly Takmazov | 2013-04-01 12:05:02 +0400 |
commit | 0623943dccad5c44831b2ad4a803eab2436e45e6 (patch) | |
tree | df9ea217f9dbfef53a82b4a7e7fc98117d1c11b3 /Juick/Classes/InvokeDelegateCommandAction.cs | |
parent | 4c92def17fef70c35e578ffb2c5e6acde3b9247d (diff) |
Using WPToolkit LongListSelector
Diffstat (limited to 'Juick/Classes/InvokeDelegateCommandAction.cs')
-rw-r--r-- | Juick/Classes/InvokeDelegateCommandAction.cs | 151 |
1 files changed, 151 insertions, 0 deletions
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<DependencyObject> + { + /// <summary> + /// + /// </summary> + public static readonly DependencyProperty CommandParameterProperty = + DependencyProperty.Register("CommandParameter", typeof(object), typeof(InvokeDelegateCommandAction), null); + + /// <summary> + /// + /// </summary> + public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( + "Command", typeof(ICommand), typeof(InvokeDelegateCommandAction), null); + + /// <summary> + /// + /// </summary> + public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register( + "InvokeParameter", typeof(object), typeof(InvokeDelegateCommandAction), null); + + private string commandName; + + /// <summary> + /// + /// </summary> + public object InvokeParameter + { + get + { + return this.GetValue(InvokeParameterProperty); + } + set + { + this.SetValue(InvokeParameterProperty, value); + } + } + + /// <summary> + /// + /// </summary> + public ICommand Command + { + get + { + return (ICommand)this.GetValue(CommandProperty); + } + set + { + this.SetValue(CommandProperty, value); + } + } + + /// <summary> + /// + /// </summary> + public string CommandName + { + get + { + return this.commandName; + } + set + { + if (this.CommandName != value) + { + this.commandName = value; + } + } + } + + /// <summary> + /// + /// </summary> + public object CommandParameter + { + get + { + return this.GetValue(CommandParameterProperty); + } + set + { + this.SetValue(CommandParameterProperty, value); + } + } + + /// <summary> + /// + /// </summary> + /// <param name="parameter"></param> + 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; + } + } +} |