123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Reflection;
- using System.Text;
- using System.Windows;
- using System.Windows.Media;
- namespace HandyControl.Interactivity
- {
- public class SetPropertyAction : TriggerAction<FrameworkElement>
- {
- // PropertyName DependencyProperty.
- /// <summary>
- /// The property to be executed in response to the trigger.
- /// </summary>
- public string PropertyName
- {
- get { return (string)GetValue(PropertyNameProperty); }
- set { SetValue(PropertyNameProperty, value); }
- }
- public static readonly DependencyProperty PropertyNameProperty
- = DependencyProperty.Register("PropertyName", typeof(string),
- typeof(SetPropertyAction));
- // PropertyValue DependencyProperty.
- /// <summary>
- /// The value to set the property to.
- /// </summary>
- public object PropertyValue
- {
- get { return GetValue(PropertyValueProperty); }
- set { SetValue(PropertyValueProperty, value); }
- }
- public static readonly DependencyProperty PropertyValueProperty
- = DependencyProperty.Register("PropertyValue", typeof(object),
- typeof(SetPropertyAction));
- // TargetObject DependencyProperty.
- /// <summary>
- /// Specifies the object upon which to set the property.
- /// </summary>
- public object TargetObject
- {
- get { return GetValue(TargetObjectProperty); }
- set { SetValue(TargetObjectProperty, value); }
- }
- public static readonly DependencyProperty TargetObjectProperty
- = DependencyProperty.Register("TargetObject", typeof(object),
- typeof(SetPropertyAction));
- // Private Implementation.
- protected override void Invoke(object parameter)
- {
- object target = TargetObject ?? AssociatedObject;
- PropertyInfo propertyInfo = target.GetType().GetProperty(
- PropertyName,
- BindingFlags.Instance | BindingFlags.Public
- | BindingFlags.NonPublic | BindingFlags.InvokeMethod);
- if (propertyInfo == null) return;
- object tempvalue = null;
- if (propertyInfo.PropertyType == typeof(bool))
- {
- tempvalue = bool.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(int))
- {
- tempvalue = int.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(uint))
- {
- tempvalue = uint.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(byte))
- {
- tempvalue = byte.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(sbyte))
- {
- tempvalue = sbyte.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(ushort))
- {
- tempvalue = ushort.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(short))
- {
- tempvalue = short.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(long))
- {
- tempvalue = long.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(ulong))
- {
- tempvalue = ulong.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(float))
- {
- tempvalue = float.Parse(PropertyValue.ToString());
- }
- else if (propertyInfo.PropertyType == typeof(double))
- {
- tempvalue = double.Parse(PropertyValue.ToString());
- }
- else if(propertyInfo.PropertyType == typeof(Brush))
- {
- tempvalue = new SolidColorBrush((Color)ColorConverter.ConvertFromString(PropertyValue.ToString()));
- }
- else tempvalue = PropertyValue;
- propertyInfo.SetValue(target, tempvalue);
- }
- }
- public class InvokeAction : TriggerAction<FrameworkElement>
- {
- // PropertyName DependencyProperty.
- /// <summary>
- /// The property to be executed in response to the trigger.
- /// </summary>
- public string MethodName
- {
- get { return (string)GetValue(MethodNameProperty); }
- set { SetValue(MethodNameProperty, value); }
- }
- public static readonly DependencyProperty MethodNameProperty
- = DependencyProperty.Register("MethodName", typeof(string),
- typeof(SetPropertyAction));
- // PropertyValue DependencyProperty.
- /// <summary>
- /// The value to set the property to.
- /// </summary>
- public object ArgumentValue
- {
- get { return GetValue(ArgumentValueProperty); }
- set { SetValue(ArgumentValueProperty, value); }
- }
- public static readonly DependencyProperty ArgumentValueProperty
- = DependencyProperty.Register("ArgumentValue", typeof(object),
- typeof(SetPropertyAction));
- // TargetObject DependencyProperty.
- /// <summary>
- /// Specifies the object upon which to set the property.
- /// </summary>
- public object TargetObject
- {
- get { return GetValue(TargetObjectProperty); }
- set { SetValue(TargetObjectProperty, value); }
- }
- public static readonly DependencyProperty TargetObjectProperty
- = DependencyProperty.Register("TargetObject", typeof(object),
- typeof(SetPropertyAction));
- // Private Implementation.
- protected override void Invoke(object parameter)
- {
- object target = TargetObject ?? AssociatedObject;
- var propertyInfo = target.GetType().GetMethod(
- MethodName,
- BindingFlags.Instance | BindingFlags.Public
- | BindingFlags.NonPublic | BindingFlags.InvokeMethod);
- if (propertyInfo == null) return;
- if(propertyInfo.GetGenericArguments().Length==0)
- {
- propertyInfo.Invoke(target,new object[0]);
- }
- else if(propertyInfo.GetGenericArguments().Length == 1)
- {
- propertyInfo.Invoke(target, new object[] { ArgumentValue });
- }
- else
- {
- throw new NotSupportedException();
- }
- }
- }
- }
|