GroupDigitalBoxControl.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /// GroupTextBoxControl.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class GroupDigitalBoxControl : UserControl
  21. {
  22. #region 属性
  23. public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(GroupDigitalBoxControl),
  24. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  25. /// <summary>
  26. /// 标题
  27. /// </summary>
  28. public string Title
  29. {
  30. get
  31. {
  32. return (string)this.GetValue(TitleProperty);
  33. }
  34. set
  35. {
  36. this.SetValue(TitleProperty, value);
  37. }
  38. }
  39. public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(GroupDigitalBoxControl),
  40. new FrameworkPropertyMetadata("deg", FrameworkPropertyMetadataOptions.AffectsRender));
  41. /// <summary>
  42. /// 单位
  43. /// </summary>
  44. public string Unit
  45. {
  46. get
  47. {
  48. return (string)this.GetValue(UnitProperty);
  49. }
  50. set
  51. {
  52. this.SetValue(UnitProperty, value);
  53. }
  54. }
  55. public static readonly DependencyProperty DigtalValueProperty = DependencyProperty.Register("DigitalValue", typeof(double), typeof(GroupDigitalBoxControl),
  56. new FrameworkPropertyMetadata((double)-1, new PropertyChangedCallback(DigitalValueChanged)));
  57. /// <summary>
  58. /// 数值
  59. /// </summary>
  60. public double DigitalValue
  61. {
  62. get
  63. {
  64. return (double)this.GetValue(DigtalValueProperty);
  65. }
  66. set
  67. {
  68. this.SetValue(DigtalValueProperty, value);
  69. }
  70. }
  71. /// <summary>
  72. /// 数值发生变化事件
  73. /// </summary>
  74. /// <param name="d"></param>
  75. /// <param name="e"></param>
  76. private static void DigitalValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  77. {
  78. if (e.NewValue != null)
  79. {
  80. double newValue = (double)e.NewValue;
  81. double minValue = (double)d.GetValue(MinValueProperty);
  82. double maxValue = (double)d.GetValue(MaxValueProperty);
  83. bool validResult = newValue >= minValue && newValue <= maxValue;
  84. d.SetValue(ValidResultProperty, validResult);
  85. }
  86. }
  87. public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(GroupDigitalBoxControl),
  88. new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
  89. /// <summary>
  90. /// 最小数值
  91. /// </summary>
  92. public double MinValue
  93. {
  94. get
  95. {
  96. return (double)this.GetValue(MinValueProperty);
  97. }
  98. set
  99. {
  100. this.SetValue(MinValueProperty, value);
  101. }
  102. }
  103. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(GroupDigitalBoxControl),
  104. new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
  105. /// <summary>
  106. /// 最大数值
  107. /// </summary>
  108. public double MaxValue
  109. {
  110. get
  111. {
  112. return (double)this.GetValue(MaxValueProperty);
  113. }
  114. set
  115. {
  116. this.SetValue(MaxValueProperty, value);
  117. }
  118. }
  119. public static readonly DependencyProperty EnableProperty = DependencyProperty.Register("Enable", typeof(bool), typeof(GroupDigitalBoxControl),
  120. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  121. /// <summary>
  122. /// 可用性
  123. /// </summary>
  124. public bool Enable
  125. {
  126. get
  127. {
  128. return (bool)this.GetValue(EnableProperty);
  129. }
  130. set
  131. {
  132. this.SetValue(EnableProperty, value);
  133. }
  134. }
  135. public static readonly DependencyProperty ValidResultProperty = DependencyProperty.Register("ValidResult", typeof(bool), typeof(GroupDigitalBoxControl),
  136. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  137. /// <summary>
  138. /// 合理性
  139. /// </summary>
  140. public bool ValidResult
  141. {
  142. get
  143. {
  144. return (bool)this.GetValue(ValidResultProperty);
  145. }
  146. set
  147. {
  148. this.SetValue(ValidResultProperty, value);
  149. }
  150. }
  151. #endregion
  152. public GroupDigitalBoxControl()
  153. {
  154. InitializeComponent();
  155. }
  156. protected override void OnRender(DrawingContext drawingContext)
  157. {
  158. base.OnRender(drawingContext);
  159. ValidResult = DigitalValue >= MinValue && DigitalValue <= MaxValue;
  160. RectangleSetPoint.Fill = ValidResult? new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Yellow);
  161. txtValue.Background = ValidResult? new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Yellow);
  162. }
  163. }
  164. }