GroupTextBoxControl.xaml.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 GroupTextBoxControl : UserControl
  21. {
  22. #region 属性
  23. public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(GroupTextBoxControl),
  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(GroupTextBoxControl),
  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 IntValueProperty = DependencyProperty.Register("IntValue", typeof(int), typeof(GroupTextBoxControl),
  56. new PropertyMetadata(-1, new PropertyChangedCallback(IntValueChanged)));
  57. /// <summary>
  58. /// 数值
  59. /// </summary>
  60. public int IntValue
  61. {
  62. get
  63. {
  64. return (int)this.GetValue(IntValueProperty);
  65. }
  66. set
  67. {
  68. this.SetValue(IntValueProperty, value);
  69. }
  70. }
  71. public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(int), typeof(GroupTextBoxControl),
  72. new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
  73. /// <summary>
  74. /// 最小数值
  75. /// </summary>
  76. public int MinValue
  77. {
  78. get
  79. {
  80. return (int)this.GetValue(MinValueProperty);
  81. }
  82. set
  83. {
  84. this.SetValue(MinValueProperty, value);
  85. }
  86. }
  87. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(int), typeof(GroupTextBoxControl),
  88. new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
  89. /// <summary>
  90. /// 最大数值
  91. /// </summary>
  92. public int MaxValue
  93. {
  94. get
  95. {
  96. return (int)this.GetValue(MaxValueProperty);
  97. }
  98. set
  99. {
  100. this.SetValue(MaxValueProperty, value);
  101. }
  102. }
  103. public static readonly DependencyProperty EnableProperty = DependencyProperty.Register("Enable", typeof(bool), typeof(GroupTextBoxControl),
  104. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  105. /// <summary>
  106. /// 可用性
  107. /// </summary>
  108. public bool Enable
  109. {
  110. get
  111. {
  112. return (bool)this.GetValue(EnableProperty);
  113. }
  114. set
  115. {
  116. this.SetValue(EnableProperty, value);
  117. }
  118. }
  119. public static readonly DependencyProperty ValidResultProperty = DependencyProperty.Register("ValidResult", typeof(bool), typeof(GroupTextBoxControl),
  120. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  121. /// <summary>
  122. /// 合理性
  123. /// </summary>
  124. public bool ValidResult
  125. {
  126. get
  127. {
  128. return (bool)this.GetValue(ValidResultProperty);
  129. }
  130. set
  131. {
  132. this.SetValue(ValidResultProperty, value);
  133. }
  134. }
  135. /// <summary>
  136. /// 数值发生变化事件
  137. /// </summary>
  138. /// <param name="d"></param>
  139. /// <param name="e"></param>
  140. private static void IntValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  141. {
  142. if(e.NewValue!=null)
  143. {
  144. int newValue = (int)e.NewValue;
  145. int minValue=(int)d.GetValue(MinValueProperty);
  146. int maxValue= (int)d.GetValue(MaxValueProperty);
  147. bool validResult= newValue >= minValue && newValue <= maxValue;
  148. d.SetValue(ValidResultProperty, validResult);
  149. }
  150. }
  151. #endregion
  152. public GroupTextBoxControl()
  153. {
  154. InitializeComponent();
  155. }
  156. protected override void OnRender(DrawingContext drawingContext)
  157. {
  158. base.OnRender(drawingContext);
  159. ValidResult = IntValue >= MinValue && IntValue <= 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. }