Valve.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using OpenSEMI.Ctrlib.Types;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace OpenSEMI.Ctrlib.Controls
  5. {
  6. public class Valve : Button
  7. {
  8. public static readonly DependencyProperty ValveStateProperty;
  9. public static readonly DependencyProperty ValueProperty;
  10. public static readonly DependencyProperty OrientationProperty;
  11. private ValveLogicType m_ValveLogic = ValveLogicType.Positive;
  12. public ValveState ValveState
  13. {
  14. get
  15. {
  16. return (ValveState)GetValue(ValveStateProperty);
  17. }
  18. set
  19. {
  20. SetValue(ValveStateProperty, value);
  21. }
  22. }
  23. public int Value
  24. {
  25. get
  26. {
  27. return (int)GetValue(ValueProperty);
  28. }
  29. set
  30. {
  31. SetValue(ValueProperty, value);
  32. }
  33. }
  34. public Orientation Orientation
  35. {
  36. get
  37. {
  38. return (Orientation)GetValue(OrientationProperty);
  39. }
  40. set
  41. {
  42. SetValue(OrientationProperty, value);
  43. }
  44. }
  45. public ValveLogicType ValveLogic
  46. {
  47. get
  48. {
  49. return m_ValveLogic;
  50. }
  51. set
  52. {
  53. m_ValveLogic = value;
  54. }
  55. }
  56. static Valve()
  57. {
  58. ValveStateProperty = DependencyProperty.Register("ValveState", typeof(ValveState), typeof(Valve), new UIPropertyMetadata(ValveState.UNKNOWN));
  59. ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(Valve), new UIPropertyMetadata(-1, ValueChangedCallBack));
  60. OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(Valve), new UIPropertyMetadata(Orientation.Vertical));
  61. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(Valve), new FrameworkPropertyMetadata(typeof(Valve)));
  62. }
  63. public override void OnApplyTemplate()
  64. {
  65. base.OnApplyTemplate();
  66. SetValveState(this);
  67. }
  68. public static void ValueChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
  69. {
  70. Valve valve = d as Valve;
  71. if (valve != null)
  72. {
  73. SetValveState(valve);
  74. }
  75. }
  76. private static void SetValveState(Valve v)
  77. {
  78. if (v.Value == 0)
  79. {
  80. if (v.ValveLogic == ValveLogicType.Positive)
  81. {
  82. v.ValveState = ValveState.OFF;
  83. }
  84. else
  85. {
  86. v.ValveState = ValveState.ON;
  87. }
  88. }
  89. else if (v.Value == 1)
  90. {
  91. if (v.ValveLogic == ValveLogicType.Positive)
  92. {
  93. v.ValveState = ValveState.ON;
  94. }
  95. else
  96. {
  97. v.ValveState = ValveState.OFF;
  98. }
  99. }
  100. else
  101. {
  102. v.ValveState = ValveState.UNKNOWN;
  103. }
  104. }
  105. }
  106. }