AITSensor.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. using System.Windows.Media;
  5. using Aitex.Core.Common.DeviceData;
  6. namespace Aitex.Core.UI.DeviceControl
  7. {
  8. /// <summary>
  9. /// AITSensor.xaml 的交互逻辑
  10. /// </summary>
  11. public partial class AITSensor : UserControl
  12. {
  13. public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
  14. "DeviceData", typeof(AITSensorData), typeof(AITSensor), new FrameworkPropertyMetadata(new AITSensorData(), FrameworkPropertyMetadataOptions.AffectsRender));
  15. public AITSensorData DeviceData
  16. {
  17. get { return (AITSensorData)this.GetValue(DeviceDataProperty); }
  18. set { this.SetValue(DeviceDataProperty, value); }
  19. }
  20. public bool GreenColor
  21. {
  22. get { return (bool)GetValue(GreenColorProperty); }
  23. set { SetValue(GreenColorProperty, value); }
  24. }
  25. // Using a DependencyProperty as the backing store for GreenColor. This enables animation, styling, binding, etc...
  26. public static readonly DependencyProperty GreenColorProperty =
  27. DependencyProperty.Register("GreenColor", typeof(bool), typeof(AITSensor), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  28. public bool EnableToolTip
  29. {
  30. get { return (bool)GetValue(EnableToolTipProperty); }
  31. set { SetValue(EnableToolTipProperty, value); }
  32. }
  33. // Using a DependencyProperty as the backing store for EnableToolTip. This enables animation, styling, binding, etc...
  34. public static readonly DependencyProperty EnableToolTipProperty =
  35. DependencyProperty.Register("EnableToolTip", typeof(bool), typeof(AITSensor), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  36. public bool LightOnValue
  37. {
  38. get { return (bool)GetValue(LightOnValueProperty); }
  39. set { SetValue(LightOnValueProperty, value); }
  40. }
  41. public static readonly DependencyProperty LightOnValueProperty =
  42. DependencyProperty.Register("LightOnValue", typeof(bool), typeof(AITSensor), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  43. public bool IsInterlockMode
  44. {
  45. get { return (bool)GetValue(IsInterlockModeProperty); }
  46. set { SetValue(IsInterlockModeProperty, value); }
  47. }
  48. public static readonly DependencyProperty IsInterlockModeProperty =
  49. DependencyProperty.Register("IsInterlockMode", typeof(bool), typeof(AITSensor), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  50. public bool IsCustomRender
  51. {
  52. get { return (bool)GetValue(IsCustomRenderProperty); }
  53. set { SetValue(IsCustomRenderProperty, value); }
  54. }
  55. public static readonly DependencyProperty IsCustomRenderProperty =
  56. DependencyProperty.Register("IsCustomRender", typeof(bool), typeof(AITSensor), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  57. public Brush CustomColorOn
  58. {
  59. get { return (Brush)GetValue(CustomColorOnProperty); }
  60. set { SetValue(CustomColorOnProperty, value); }
  61. }
  62. public static readonly DependencyProperty CustomColorOnProperty =
  63. DependencyProperty.Register("CustomColorOn", typeof(Brush), typeof(AITSensor), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Lime), FrameworkPropertyMetadataOptions.AffectsRender));
  64. public Brush CustomColorOff
  65. {
  66. get { return (Brush)GetValue(CustomColorOffProperty); }
  67. set { SetValue(CustomColorOffProperty, value); }
  68. }
  69. public static readonly DependencyProperty CustomColorOffProperty =
  70. DependencyProperty.Register("CustomColorOff", typeof(Brush), typeof(AITSensor), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Gray), FrameworkPropertyMetadataOptions.AffectsRender));
  71. public AITSensor()
  72. {
  73. InitializeComponent();
  74. }
  75. protected override void OnRender(DrawingContext drawingContext)
  76. {
  77. base.OnRender(drawingContext);
  78. if (IsCustomRender)
  79. {
  80. if (DeviceData == null || string.IsNullOrEmpty(DeviceData.DeviceName))
  81. {
  82. sensor.Fill = LightOnValue? CustomColorOn : CustomColorOff;
  83. }
  84. else
  85. {
  86. sensor.Fill = DeviceData.Value ? CustomColorOn : CustomColorOff;
  87. }
  88. return;
  89. }
  90. if (DeviceData == null || string.IsNullOrEmpty(DeviceData.DeviceName))
  91. {
  92. if (LightOnValue)
  93. sensor.Fill = (GreenColor ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF07FF07")) : new SolidColorBrush(Colors.Red));
  94. else
  95. sensor.Fill = new SolidColorBrush(Colors.Gray);
  96. return;
  97. }
  98. if (IsInterlockMode)
  99. {
  100. sensor.Fill = DeviceData.Value ? new SolidColorBrush(Colors.Lime) : new SolidColorBrush(Colors.Red);
  101. }
  102. else
  103. {
  104. if (DeviceData.IsError)
  105. {
  106. sensor.Fill = new SolidColorBrush(Colors.Red);
  107. }
  108. else
  109. {
  110. sensor.Fill = DeviceData.Value ? new SolidColorBrush(Colors.Lime) : new SolidColorBrush(Colors.Gray);
  111. }
  112. }
  113. }
  114. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  115. {
  116. if (DeviceData != null && EnableToolTip)
  117. {
  118. ToolTip = $"Sensor \r\n {DeviceData.DisplayName} \r\n {DeviceData.Value}";
  119. }
  120. }
  121. }
  122. }