星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

UC_MultiRangeSlider.xaml.cs 6.8KB

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. namespace XHWK.WKTool
  6. {
  7. /// <summary>
  8. /// UC_MultiRangeSlider.xaml 的交互逻辑
  9. /// </summary>
  10. public partial class UC_MultiRangeSlider : UserControl
  11. {
  12. public UC_MultiRangeSlider()
  13. {
  14. InitializeComponent();
  15. }
  16. #region 私有变量
  17. private static int _width = 150; // 拖动条初始宽度
  18. private static int _height = 30; // 高度
  19. private static double _min = 0; // 最小值
  20. private static double _max = 1000; // 最大值
  21. private static int _freq = 1; // 出现刻度的间距
  22. #endregion
  23. /// <summary>
  24. /// 裁剪矩阵(头)
  25. /// </summary>
  26. private Rect StartRect
  27. {
  28. get
  29. {
  30. return (Rect)GetValue(StartRectProperty);
  31. }
  32. set
  33. {
  34. SetValue(StartRectProperty, value);
  35. }
  36. }
  37. private static readonly DependencyProperty StartRectProperty =
  38. DependencyProperty.Register("StartRect", typeof(Rect), typeof(UC_MultiRangeSlider));
  39. /// <summary>
  40. /// 裁剪矩阵(尾)
  41. /// </summary>
  42. private Rect EndRect
  43. {
  44. get { return (Rect)GetValue(EndRectProperty); }
  45. set { SetValue(EndRectProperty, value); }
  46. }
  47. private static readonly DependencyProperty EndRectProperty =
  48. DependencyProperty.Register("EndRect", typeof(Rect), typeof(UC_MultiRangeSlider));
  49. #region 公开依赖属性
  50. /// <summary>
  51. /// 刻度间距,默认为10
  52. /// </summary>
  53. public int SliderTickFrequency
  54. {
  55. get { return (int)GetValue(SliderTickFrequencyProperty); }
  56. set { SetValue(SliderTickFrequencyProperty, value); }
  57. }
  58. public static readonly DependencyProperty SliderTickFrequencyProperty =
  59. DependencyProperty.Register("SliderTickFrequency", typeof(int), typeof(UC_MultiRangeSlider), new PropertyMetadata(_freq));
  60. /// <summary>
  61. /// 控件高度,默认为30
  62. /// </summary>
  63. public int SilderHeight
  64. {
  65. get { return (int)GetValue(SilderHeightProperty); }
  66. set { SetValue(SilderHeightProperty, value); }
  67. }
  68. public static readonly DependencyProperty SilderHeightProperty =
  69. DependencyProperty.Register("SilderHeight", typeof(int), typeof(UC_MultiRangeSlider), new PropertyMetadata(_height));
  70. /// <summary>
  71. /// 拖动条宽度,默认为150
  72. /// </summary>
  73. public int SilderWidth
  74. {
  75. get { return (int)GetValue(SilderWidthProperty); }
  76. set { SetValue(SilderWidthProperty, value); }
  77. }
  78. public static readonly DependencyProperty SilderWidthProperty =
  79. DependencyProperty.Register("SilderWidth", typeof(int), typeof(UC_MultiRangeSlider), new PropertyMetadata(_width));
  80. /// <summary>
  81. /// 最小值,默认为0
  82. /// </summary>
  83. public double Minimum
  84. {
  85. get { return (double)GetValue(MinimumProperty); }
  86. set { SetValue(MinimumProperty, value); }
  87. }
  88. public static readonly DependencyProperty MinimumProperty =
  89. DependencyProperty.Register("Minimum", typeof(double), typeof(UC_MultiRangeSlider), new PropertyMetadata(_min));
  90. /// <summary>
  91. /// 最大值,默认为100
  92. /// </summary>
  93. public double Maximum
  94. {
  95. get { return (double)GetValue(MaximumProperty); }
  96. set { SetValue(MaximumProperty, value); }
  97. }
  98. public static readonly DependencyProperty MaximumProperty =
  99. DependencyProperty.Register("Maximum", typeof(double), typeof(UC_MultiRangeSlider), new PropertyMetadata(_max));
  100. /// <summary>
  101. /// 选中开始值,默认为0
  102. /// </summary>
  103. public double StartValue
  104. {
  105. get { return (double)GetValue(StartValueProperty); }
  106. set { SetValue(StartValueProperty, value); }
  107. }
  108. public static readonly DependencyProperty StartValueProperty =
  109. DependencyProperty.Register("StartValue", typeof(double), typeof(UC_MultiRangeSlider));
  110. /// <summary>
  111. /// 选中结束值,默认为100
  112. /// </summary>
  113. public double EndValue
  114. {
  115. get { return (double)GetValue(EndValueProperty); }
  116. set { SetValue(EndValueProperty, value); }
  117. }
  118. public static readonly DependencyProperty EndValueProperty =
  119. DependencyProperty.Register("EndValue", typeof(double), typeof(UC_MultiRangeSlider), new PropertyMetadata(_max));
  120. #endregion
  121. #region 前台交互
  122. /// <summary>
  123. /// 对两个拖动条进行裁剪
  124. /// </summary>
  125. private void ClipSilder()
  126. {
  127. double selectedValue = EndValue - StartValue;
  128. double totalValue = Maximum - Minimum;
  129. double sliderClipWidth = (double)SilderWidth * (StartValue - Minimum + selectedValue / 2.0) / totalValue;
  130. // 对第一个拖动条进行裁剪
  131. StartRect = new Rect(0, 0, sliderClipWidth, SilderHeight);
  132. // 对第二个拖动条进行裁剪
  133. EndRect = new Rect(sliderClipWidth, 0, SilderWidth, SilderHeight);
  134. }
  135. /// <summary>
  136. /// 初始化裁剪
  137. /// </summary>
  138. private void UC_Arrange_Loaded(object sender, RoutedEventArgs e)
  139. {
  140. ClipSilder();
  141. }
  142. private void SL_Bat1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  143. {
  144. if (e.NewValue > EndValue) // 检查值范围
  145. StartValue = EndValue; // 超出,重设为最大值
  146. ClipSilder();
  147. }
  148. private void SL_Bat2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  149. {
  150. if (e.NewValue < StartValue)
  151. EndValue = StartValue;
  152. ClipSilder();
  153. }
  154. private void TextBox_KeyUp1(object sender, System.Windows.Input.KeyEventArgs e)
  155. {
  156. try
  157. {
  158. if (e.Key == Key.Enter) // 按回车时确认输入
  159. StartValue = Convert.ToInt32(((TextBox)sender).Text);
  160. }
  161. catch (Exception ex)
  162. {
  163. MessageBox.Show(ex.Message);
  164. }
  165. }
  166. private void TextBox_KeyUp2(object sender, KeyEventArgs e)
  167. {
  168. try
  169. {
  170. if (e.Key == Key.Enter)
  171. EndValue = Convert.ToInt32(((TextBox)sender).Text);
  172. }
  173. catch (Exception ex)
  174. {
  175. MessageBox.Show(ex.Message);
  176. }
  177. }
  178. #endregion
  179. }
  180. }