Valve.cs 3.3 KB

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