PropertyCommandTrigger.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. namespace OpenSEMI.ClientBase.Command
  9. {
  10. public class PropertyCommandTrigger : CommandTrigger
  11. {
  12. #region Dependency Properties
  13. #region Property Property
  14. /// <value>Identifies the Property dependency property</value>
  15. public static readonly DependencyProperty PropertyProperty =
  16. DependencyProperty.Register("Property", typeof(DependencyProperty), typeof(PropertyCommandTrigger),
  17. new FrameworkPropertyMetadata(null));
  18. /// <value>description for Property property</value>
  19. public DependencyProperty Property
  20. {
  21. get { return (DependencyProperty)GetValue(PropertyProperty); }
  22. set { SetValue(PropertyProperty, value); }
  23. }
  24. #endregion
  25. #region Value Property
  26. /// <value>Identifies the Value dependency property</value>
  27. public static readonly DependencyProperty ValueProperty =
  28. DependencyProperty.Register("Value", typeof(string), typeof(PropertyCommandTrigger),
  29. new FrameworkPropertyMetadata(null));
  30. /// <value>description for Value property</value>
  31. public string Value
  32. {
  33. get { return (string)GetValue(ValueProperty); }
  34. set { SetValue(ValueProperty, value); }
  35. }
  36. #endregion
  37. #endregion
  38. protected override Freezable CreateInstanceCore()
  39. {
  40. return new PropertyCommandTrigger();
  41. }
  42. #region T Property
  43. /// <value>Identifies the T dependency property</value>
  44. public static readonly DependencyProperty TProperty =
  45. DependencyProperty.Register("T", typeof(object), typeof(PropertyCommandTrigger),
  46. new FrameworkPropertyMetadata(null, OnTChanged));
  47. /// <value>description for T property</value>
  48. public object T
  49. {
  50. get { return (object)GetValue(TProperty); }
  51. set { SetValue(TProperty, value); }
  52. }
  53. /// <summary>
  54. /// Invoked on T change.
  55. /// </summary>
  56. /// <param name="d">The object that was changed</param>
  57. /// <param name="e">Dependency property changed event arguments</param>
  58. static void OnTChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  59. {
  60. }
  61. #endregion
  62. protected override void InitializeCore(FrameworkElement source)
  63. {
  64. DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(Property, source.GetType());
  65. descriptor.AddValueChanged(source, (s, e) =>
  66. {
  67. CommandParameter<object> parameter = new PropertyCommandParameter<object, object>(
  68. CustomParameter, Property, source.GetValue(Property));
  69. object value = Value;
  70. if (descriptor.Converter.CanConvertFrom(typeof(string)))
  71. {
  72. value = descriptor.Converter.ConvertFromString(Value);
  73. }
  74. if (object.Equals(source.GetValue(Property), value))
  75. ExecuteCommand(parameter);
  76. });
  77. }
  78. }
  79. }