AITSensor.xaml.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 LightOnValue
  29. {
  30. get { return (bool)GetValue(LightOnValueProperty); }
  31. set { SetValue(LightOnValueProperty, value); }
  32. }
  33. public static readonly DependencyProperty LightOnValueProperty =
  34. DependencyProperty.Register("LightOnValue", typeof(bool), typeof(AITSensor), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  35. public bool IsInterlockMode
  36. {
  37. get { return (bool)GetValue(IsInterlockModeProperty); }
  38. set { SetValue(IsInterlockModeProperty, value); }
  39. }
  40. public static readonly DependencyProperty IsInterlockModeProperty =
  41. DependencyProperty.Register("IsInterlockMode", typeof(bool), typeof(AITSensor), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  42. public AITSensor()
  43. {
  44. InitializeComponent();
  45. }
  46. protected override void OnRender(DrawingContext drawingContext)
  47. {
  48. base.OnRender(drawingContext);
  49. if (DeviceData == null || string.IsNullOrEmpty(DeviceData.DeviceName))
  50. {
  51. if (LightOnValue)
  52. sensor.Fill = (GreenColor ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF07FF07")) : new SolidColorBrush(Colors.Red));
  53. else
  54. sensor.Fill = new SolidColorBrush(Colors.Gray);
  55. return;
  56. }
  57. if (IsInterlockMode)
  58. {
  59. sensor.Fill = DeviceData.Value ? new SolidColorBrush(Colors.Lime) : new SolidColorBrush(Colors.Red);
  60. }
  61. else
  62. {
  63. if (DeviceData.IsError)
  64. {
  65. sensor.Fill = new SolidColorBrush(Colors.Red);
  66. }
  67. else
  68. {
  69. sensor.Fill = DeviceData.Value ? new SolidColorBrush(Colors.Lime) : new SolidColorBrush(Colors.Gray);
  70. }
  71. }
  72. }
  73. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  74. {
  75. if (DeviceData != null)
  76. {
  77. ToolTip = $"Sensor \r\n {DeviceData.DisplayName} \r\n {DeviceData.Value}";
  78. }
  79. }
  80. }
  81. }