PressureBlockControl.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 CyberX8_Themes.UserControls
  16. {
  17. /// <summary>
  18. /// PressureBlockControl.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class PressureBlockControl : UserControl
  21. {
  22. #region 属性
  23. public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PressureBlockControl),
  24. new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
  25. /// <summary>
  26. /// 数值
  27. /// </summary>
  28. public double Value
  29. {
  30. get
  31. {
  32. return (double)this.GetValue(ValueProperty);
  33. }
  34. set
  35. {
  36. this.SetValue(ValueProperty, value);
  37. }
  38. }
  39. public static readonly DependencyProperty ForeColorProperty = DependencyProperty.Register("ForeColor", typeof(SolidColorBrush), typeof(PressureBlockControl),
  40. new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Lime), FrameworkPropertyMetadataOptions.AffectsRender));
  41. /// <summary>
  42. /// 文字颜色
  43. /// </summary>
  44. public SolidColorBrush ForeColor
  45. {
  46. get
  47. {
  48. return (SolidColorBrush)this.GetValue(ForeColorProperty);
  49. }
  50. set
  51. {
  52. this.SetValue(ForeColorProperty, value);
  53. }
  54. }
  55. public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(SolidColorBrush), typeof(PressureBlockControl),
  56. new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Black), FrameworkPropertyMetadataOptions.AffectsRender));
  57. /// <summary>
  58. /// 背景颜色
  59. /// </summary>
  60. public SolidColorBrush BackgroundColor
  61. {
  62. get
  63. {
  64. return (SolidColorBrush)this.GetValue(BackgroundColorProperty);
  65. }
  66. set
  67. {
  68. this.SetValue(BackgroundColorProperty, value);
  69. }
  70. }
  71. public static readonly DependencyProperty IsWarningProperty = DependencyProperty.Register("IsWarning", typeof(bool), typeof(PressureBlockControl),
  72. new FrameworkPropertyMetadata(false, new PropertyChangedCallback(IsWarningPropertyChanged)));
  73. /// <summary>
  74. /// 是否告警
  75. /// </summary>
  76. public bool IsWarning
  77. {
  78. get
  79. {
  80. return (bool)this.GetValue(IsWarningProperty);
  81. }
  82. set
  83. {
  84. this.SetValue(IsWarningProperty, value);
  85. }
  86. }
  87. public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register("IsError", typeof(bool), typeof(PressureBlockControl),
  88. new FrameworkPropertyMetadata(false, new PropertyChangedCallback(IsErrorPropertyChanged)));
  89. /// <summary>
  90. /// 是否错误
  91. /// </summary>
  92. public bool IsError
  93. {
  94. get
  95. {
  96. return (bool)this.GetValue(IsErrorProperty);
  97. }
  98. set
  99. {
  100. this.SetValue(IsErrorProperty, value);
  101. }
  102. }
  103. private static void IsWarningPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  104. {
  105. if(e.NewValue!=null)
  106. {
  107. bool isWarning=(bool)e.NewValue;
  108. bool isError = (bool)d.GetValue(IsErrorProperty);
  109. if(isWarning)
  110. {
  111. if (!isError)
  112. {
  113. d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Yellow));
  114. d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Black));
  115. }
  116. }
  117. else
  118. {
  119. if (!isError)
  120. {
  121. d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Black));
  122. d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Lime));
  123. }
  124. }
  125. }
  126. }
  127. private static void IsErrorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  128. {
  129. if (e.NewValue != null)
  130. {
  131. bool isError = (bool)e.NewValue;
  132. bool isWarning = (bool)d.GetValue(IsWarningProperty);
  133. if (isError)
  134. {
  135. d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Red));
  136. d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Black));
  137. }
  138. else
  139. {
  140. if (!isWarning)
  141. {
  142. d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Black));
  143. d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Lime));
  144. }
  145. }
  146. }
  147. }
  148. #endregion
  149. public PressureBlockControl()
  150. {
  151. InitializeComponent();
  152. }
  153. }
  154. }