StateInfoPoint.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace NEIntelligentControl2.Views.Infos
  16. {
  17. /// <summary>
  18. /// 测点
  19. /// </summary>
  20. public partial class StateInfoPoint : Border
  21. {
  22. public static readonly DependencyProperty TotalTimeProperty = DependencyProperty.Register("TotalTime", typeof(double), typeof(StateInfoPoint), new PropertyMetadata(ValueChanged));
  23. public static readonly DependencyProperty TimeProperty = DependencyProperty.Register("Time", typeof(double), typeof(StateInfoPoint), new PropertyMetadata(ValueChanged));
  24. public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(string), typeof(StateInfoPoint), new PropertyMetadata(StateChanged));
  25. public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(StateInfoPoint), new PropertyMetadata(OnSelectionChanged));
  26. /// <summary>
  27. /// 总时间
  28. /// </summary>
  29. public double TotalTime { get => (double)GetValue(TotalTimeProperty); set => SetValue(TotalTimeProperty, value); }
  30. /// <summary>
  31. /// 时间
  32. /// </summary>
  33. public double Time { get => (double)GetValue(TimeProperty); set => SetValue(TimeProperty, Math.Round(value, 2)); }
  34. /// <summary>
  35. /// 状态
  36. /// </summary>
  37. public string State { get => (string)GetValue(StateProperty); set => SetValue(StateProperty, value); }
  38. /// <summary>
  39. /// 是否选中
  40. /// </summary>
  41. public bool IsSelected { get => (bool)GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); }
  42. /// <summary>
  43. /// 总宽度
  44. /// </summary>
  45. public double TotalWidth { get; set; }
  46. /// <summary>
  47. /// 百分比
  48. /// </summary>
  49. public double Percentage { get; set; }
  50. /// <summary>
  51. /// 选择改变
  52. /// </summary>
  53. public Action<bool, double> SelectionChanged { get; set; }
  54. public Models.Windturbine.StatusTimeInfo Info { get; set; }
  55. public StateInfoPoint(Models.Windturbine.StatusTimeInfo v)
  56. {
  57. InitializeComponent();
  58. Info = v;
  59. State = v.Status;
  60. Time = v.Duration;
  61. }
  62. /// <summary>
  63. /// 数据更改
  64. /// </summary>
  65. private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  66. {
  67. if (e.NewValue == e.OldValue) return;
  68. StateInfoPoint si = d as StateInfoPoint;
  69. if (si.TotalTime == 0.0) return;
  70. si.Width = si.Time / si.TotalTime * si.TotalWidth;
  71. var p = si.Time / si.TotalTime;
  72. si.Percentage = p;
  73. si.ToolTip = $"{si.Info.StatusName} 时长:{si.Time} 占比:{p.ToString("p")} 时间:{si.Info.StartTime}至{si.Info.EndTime}";
  74. }
  75. /// <summary>
  76. /// 选择改变
  77. /// </summary>
  78. private static void OnSelectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  79. {
  80. StateInfoPoint si = d as StateInfoPoint;
  81. bool b = (bool)e.NewValue;
  82. Color c = b ? Color.FromRgb(0x22, 0x7E, 0xDB) : Color.FromRgb(0xF5, 0xF5, 0xF5);
  83. si.BorderBrush = new SolidColorBrush(c);
  84. si?.SelectionChanged?.Invoke(b, si.Time);
  85. }
  86. /// <summary>
  87. /// 状态改变
  88. /// </summary>
  89. private static void StateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  90. {
  91. StateInfoPoint si = d as StateInfoPoint;
  92. switch (e.NewValue)
  93. {
  94. case "Stop":
  95. si.Background = new SolidColorBrush(Color.FromRgb(0xB0, 0x97, 0x3F));
  96. break;
  97. case "OnPower":
  98. si.Background = new SolidColorBrush(Color.FromRgb(0xCF, 0xF1, 0xE8));
  99. break;
  100. case "Standby":
  101. si.Background = new SolidColorBrush(Color.FromRgb(0xCF, 0xF1, 0xE8));
  102. break;
  103. case "Start":
  104. si.Background = new SolidColorBrush(Color.FromRgb(0xCF, 0xF1, 0xE8));
  105. break;
  106. case "Online":
  107. si.Background = new SolidColorBrush(Color.FromRgb(0xC8, 0xEA, 0xFF));
  108. break;
  109. case "Fault":
  110. si.Background = new SolidColorBrush(Color.FromRgb(0xDB, 0x33, 0x33));
  111. break;
  112. case "Maintain":
  113. si.Background = new SolidColorBrush(Color.FromRgb(0xFF, 0x72, 0x0E));
  114. break;
  115. default:
  116. si.Background = new SolidColorBrush(Color.FromRgb(0xC7, 0xD3, 0xDD));
  117. break;
  118. }
  119. }
  120. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  121. {
  122. this.IsSelected = !this.IsSelected;
  123. }
  124. }
  125. }