SetterAction.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Media;
  8. namespace HandyControl.Interactivity
  9. {
  10. public class SetPropertyAction : TriggerAction<FrameworkElement>
  11. {
  12. // PropertyName DependencyProperty.
  13. /// <summary>
  14. /// The property to be executed in response to the trigger.
  15. /// </summary>
  16. public string PropertyName
  17. {
  18. get { return (string)GetValue(PropertyNameProperty); }
  19. set { SetValue(PropertyNameProperty, value); }
  20. }
  21. public static readonly DependencyProperty PropertyNameProperty
  22. = DependencyProperty.Register("PropertyName", typeof(string),
  23. typeof(SetPropertyAction));
  24. // PropertyValue DependencyProperty.
  25. /// <summary>
  26. /// The value to set the property to.
  27. /// </summary>
  28. public object PropertyValue
  29. {
  30. get { return GetValue(PropertyValueProperty); }
  31. set { SetValue(PropertyValueProperty, value); }
  32. }
  33. public static readonly DependencyProperty PropertyValueProperty
  34. = DependencyProperty.Register("PropertyValue", typeof(object),
  35. typeof(SetPropertyAction));
  36. // TargetObject DependencyProperty.
  37. /// <summary>
  38. /// Specifies the object upon which to set the property.
  39. /// </summary>
  40. public object TargetObject
  41. {
  42. get { return GetValue(TargetObjectProperty); }
  43. set { SetValue(TargetObjectProperty, value); }
  44. }
  45. public static readonly DependencyProperty TargetObjectProperty
  46. = DependencyProperty.Register("TargetObject", typeof(object),
  47. typeof(SetPropertyAction));
  48. // Private Implementation.
  49. protected override void Invoke(object parameter)
  50. {
  51. object target = TargetObject ?? AssociatedObject;
  52. PropertyInfo propertyInfo = target.GetType().GetProperty(
  53. PropertyName,
  54. BindingFlags.Instance | BindingFlags.Public
  55. | BindingFlags.NonPublic | BindingFlags.InvokeMethod);
  56. if (propertyInfo == null) return;
  57. object tempvalue = null;
  58. if (propertyInfo.PropertyType == typeof(bool))
  59. {
  60. tempvalue = bool.Parse(PropertyValue.ToString());
  61. }
  62. else if (propertyInfo.PropertyType == typeof(int))
  63. {
  64. tempvalue = int.Parse(PropertyValue.ToString());
  65. }
  66. else if (propertyInfo.PropertyType == typeof(uint))
  67. {
  68. tempvalue = uint.Parse(PropertyValue.ToString());
  69. }
  70. else if (propertyInfo.PropertyType == typeof(byte))
  71. {
  72. tempvalue = byte.Parse(PropertyValue.ToString());
  73. }
  74. else if (propertyInfo.PropertyType == typeof(sbyte))
  75. {
  76. tempvalue = sbyte.Parse(PropertyValue.ToString());
  77. }
  78. else if (propertyInfo.PropertyType == typeof(ushort))
  79. {
  80. tempvalue = ushort.Parse(PropertyValue.ToString());
  81. }
  82. else if (propertyInfo.PropertyType == typeof(short))
  83. {
  84. tempvalue = short.Parse(PropertyValue.ToString());
  85. }
  86. else if (propertyInfo.PropertyType == typeof(long))
  87. {
  88. tempvalue = long.Parse(PropertyValue.ToString());
  89. }
  90. else if (propertyInfo.PropertyType == typeof(ulong))
  91. {
  92. tempvalue = ulong.Parse(PropertyValue.ToString());
  93. }
  94. else if (propertyInfo.PropertyType == typeof(float))
  95. {
  96. tempvalue = float.Parse(PropertyValue.ToString());
  97. }
  98. else if (propertyInfo.PropertyType == typeof(double))
  99. {
  100. tempvalue = double.Parse(PropertyValue.ToString());
  101. }
  102. else if(propertyInfo.PropertyType == typeof(Brush))
  103. {
  104. tempvalue = new SolidColorBrush((Color)ColorConverter.ConvertFromString(PropertyValue.ToString()));
  105. }
  106. else tempvalue = PropertyValue;
  107. propertyInfo.SetValue(target, tempvalue);
  108. }
  109. }
  110. public class InvokeAction : TriggerAction<FrameworkElement>
  111. {
  112. // PropertyName DependencyProperty.
  113. /// <summary>
  114. /// The property to be executed in response to the trigger.
  115. /// </summary>
  116. public string MethodName
  117. {
  118. get { return (string)GetValue(MethodNameProperty); }
  119. set { SetValue(MethodNameProperty, value); }
  120. }
  121. public static readonly DependencyProperty MethodNameProperty
  122. = DependencyProperty.Register("MethodName", typeof(string),
  123. typeof(SetPropertyAction));
  124. // PropertyValue DependencyProperty.
  125. /// <summary>
  126. /// The value to set the property to.
  127. /// </summary>
  128. public object ArgumentValue
  129. {
  130. get { return GetValue(ArgumentValueProperty); }
  131. set { SetValue(ArgumentValueProperty, value); }
  132. }
  133. public static readonly DependencyProperty ArgumentValueProperty
  134. = DependencyProperty.Register("ArgumentValue", typeof(object),
  135. typeof(SetPropertyAction));
  136. // TargetObject DependencyProperty.
  137. /// <summary>
  138. /// Specifies the object upon which to set the property.
  139. /// </summary>
  140. public object TargetObject
  141. {
  142. get { return GetValue(TargetObjectProperty); }
  143. set { SetValue(TargetObjectProperty, value); }
  144. }
  145. public static readonly DependencyProperty TargetObjectProperty
  146. = DependencyProperty.Register("TargetObject", typeof(object),
  147. typeof(SetPropertyAction));
  148. // Private Implementation.
  149. protected override void Invoke(object parameter)
  150. {
  151. object target = TargetObject ?? AssociatedObject;
  152. var propertyInfo = target.GetType().GetMethod(
  153. MethodName,
  154. BindingFlags.Instance | BindingFlags.Public
  155. | BindingFlags.NonPublic | BindingFlags.InvokeMethod);
  156. if (propertyInfo == null) return;
  157. if(propertyInfo.GetGenericArguments().Length==0)
  158. {
  159. propertyInfo.Invoke(target,new object[0]);
  160. }
  161. else if(propertyInfo.GetGenericArguments().Length == 1)
  162. {
  163. propertyInfo.Invoke(target, new object[] { ArgumentValue });
  164. }
  165. else
  166. {
  167. throw new NotSupportedException();
  168. }
  169. }
  170. }
  171. }