PropertyCommandTrigger.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.ComponentModel;
  2. using System.Windows;
  3. namespace OpenSEMI.ClientBase.Command
  4. {
  5. public class PropertyCommandTrigger : CommandTrigger
  6. {
  7. public static readonly DependencyProperty PropertyProperty = DependencyProperty.Register("Property", typeof(DependencyProperty), typeof(PropertyCommandTrigger), new FrameworkPropertyMetadata(null));
  8. public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(PropertyCommandTrigger), new FrameworkPropertyMetadata(null));
  9. public static readonly DependencyProperty TProperty = DependencyProperty.Register("T", typeof(object), typeof(PropertyCommandTrigger), new FrameworkPropertyMetadata(null, OnTChanged));
  10. public DependencyProperty Property
  11. {
  12. get
  13. {
  14. return (DependencyProperty)GetValue(PropertyProperty);
  15. }
  16. set
  17. {
  18. SetValue(PropertyProperty, value);
  19. }
  20. }
  21. public string Value
  22. {
  23. get
  24. {
  25. return (string)GetValue(ValueProperty);
  26. }
  27. set
  28. {
  29. SetValue(ValueProperty, value);
  30. }
  31. }
  32. public object T
  33. {
  34. get
  35. {
  36. return GetValue(TProperty);
  37. }
  38. set
  39. {
  40. SetValue(TProperty, value);
  41. }
  42. }
  43. protected override Freezable CreateInstanceCore()
  44. {
  45. return new PropertyCommandTrigger();
  46. }
  47. private static void OnTChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  48. {
  49. }
  50. protected override void InitializeCore(FrameworkElement source)
  51. {
  52. DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(Property, source.GetType());
  53. descriptor.AddValueChanged(source, delegate
  54. {
  55. CommandParameter<object> parameter = new PropertyCommandParameter<object, object>(base.CustomParameter, Property, source.GetValue(Property));
  56. object objB = Value;
  57. if (descriptor.Converter.CanConvertFrom(typeof(string)))
  58. {
  59. objB = descriptor.Converter.ConvertFromString(Value);
  60. }
  61. if (object.Equals(source.GetValue(Property), objB))
  62. {
  63. ExecuteCommand(parameter);
  64. }
  65. });
  66. }
  67. }
  68. }