25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
8.5 KiB

  1. using BPA.UIControl.Commons.KnownBoxes;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Controls.Primitives;
  5. using System.Windows.Documents;
  6. using System.Windows.Media;
  7. namespace BPA.UIControl
  8. {
  9. /// <summary>
  10. /// Slider 帮助类
  11. /// </summary>
  12. public class SliderHelper
  13. {
  14. /// <summary>
  15. /// 选择范围颜色
  16. /// </summary>
  17. public static readonly DependencyProperty SelectionRangeBrushProperty = DependencyProperty.RegisterAttached(
  18. "SelectionRangeBrush", typeof(Brush), typeof(SliderHelper), new PropertyMetadata(default(Brush)));
  19. /// <summary>
  20. /// Sets the selection range brush.
  21. /// </summary>
  22. /// <param name="element">The element.</param>
  23. /// <param name="value">The value.</param>
  24. public static void SetSelectionRangeBrush(DependencyObject element, Brush value)
  25. {
  26. element.SetValue(SelectionRangeBrushProperty, value);
  27. }
  28. /// <summary>
  29. /// Gets the selection range brush.
  30. /// </summary>
  31. /// <param name="element">The element.</param>
  32. /// <returns>A Brush.</returns>
  33. public static Brush GetSelectionRangeBrush(DependencyObject element)
  34. {
  35. return (Brush)element.GetValue(SelectionRangeBrushProperty);
  36. }
  37. /// <summary>
  38. /// 当前值显示位置
  39. /// </summary>
  40. public static readonly DependencyProperty ValuePlacementProperty = DependencyProperty.RegisterAttached(
  41. "ValuePlacement", typeof(Dock), typeof(SliderHelper), new PropertyMetadata(default(Dock)));
  42. /// <summary>
  43. /// Sets the value placement.
  44. /// </summary>
  45. /// <param name="element">The element.</param>
  46. /// <param name="value">The value.</param>
  47. public static void SetValuePlacement(DependencyObject element, Dock value)
  48. {
  49. element.SetValue(ValuePlacementProperty, value);
  50. }
  51. /// <summary>
  52. /// Gets the value placement.
  53. /// </summary>
  54. /// <param name="element">The element.</param>
  55. /// <returns>A Dock.</returns>
  56. public static Dock GetValuePlacement(DependencyObject element)
  57. {
  58. return (Dock)element.GetValue(ValuePlacementProperty);
  59. }
  60. /// <summary>
  61. /// 当前值显示位置偏移
  62. /// </summary>
  63. public static readonly DependencyProperty ValueOffsetProperty = DependencyProperty.RegisterAttached(
  64. "ValueOffset", typeof(double), typeof(SliderHelper), new PropertyMetadata(default(double)));
  65. /// <summary>
  66. /// Sets the value offset.
  67. /// </summary>
  68. /// <param name="element">The element.</param>
  69. /// <param name="value">The value.</param>
  70. public static void SetValueOffset(DependencyObject element, double value)
  71. {
  72. element.SetValue(ValueOffsetProperty, value);
  73. }
  74. /// <summary>
  75. /// Gets the value offset.
  76. /// </summary>
  77. /// <param name="element">The element.</param>
  78. /// <returns>A double.</returns>
  79. public static double GetValueOffset(DependencyObject element)
  80. {
  81. return (double)element.GetValue(ValueOffsetProperty);
  82. }
  83. /// <summary>
  84. /// 拖拽时显示当前值
  85. /// </summary>
  86. public static readonly DependencyProperty DraggingShowValueProperty = DependencyProperty.RegisterAttached(
  87. "DraggingShowValue", typeof(bool), typeof(SliderHelper), new PropertyMetadata(BooleanBoxes.FalseBox, OnDraggingShowValueChanged));
  88. private static void OnDraggingShowValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  89. {
  90. if (d is Slider slider && GetDraggingShowValue(slider))
  91. {
  92. if (slider.IsLoaded)
  93. {
  94. AddCurrentValueAdorner(slider);
  95. }
  96. else
  97. {
  98. slider.Loaded += Slider_Loaded;
  99. }
  100. }
  101. }
  102. private static void Slider_Loaded(object sender, RoutedEventArgs e)
  103. {
  104. var slider = sender as Slider;
  105. slider.Loaded -= Slider_Loaded;
  106. AddCurrentValueAdorner(slider);
  107. }
  108. private static void AddCurrentValueAdorner(Slider slider)
  109. {
  110. if (slider.Template.FindName("Thumb", slider) is Thumb grip)
  111. {
  112. var adornerLayer = AdornerLayer.GetAdornerLayer(grip);
  113. grip.MouseEnter += (a, b) =>
  114. {
  115. var placement = GetValuePlacement(slider);
  116. var offset = GetValueOffset(slider);
  117. var stringFormat = GetValueStringFormat(slider);
  118. adornerLayer.Add(new SliderValueAdorner(grip, slider, placement, offset, stringFormat));
  119. };
  120. grip.MouseLeave += (a, b) =>
  121. {
  122. Adorner[] toRemoveArray = adornerLayer.GetAdorners(grip);
  123. if (toRemoveArray != null)
  124. {
  125. for (int x = 0; x < toRemoveArray.Length; x++)
  126. {
  127. adornerLayer.Remove(toRemoveArray[x]);
  128. }
  129. }
  130. };
  131. }
  132. }
  133. /// <summary>
  134. /// Sets the dragging show value.
  135. /// </summary>
  136. /// <param name="element">The element.</param>
  137. /// <param name="value">If true, value.</param>
  138. public static void SetDraggingShowValue(DependencyObject element, bool value)
  139. {
  140. element.SetValue(DraggingShowValueProperty, BooleanBoxes.Box(value));
  141. }
  142. /// <summary>
  143. /// Gets the dragging show value.
  144. /// </summary>
  145. /// <param name="element">The element.</param>
  146. /// <returns>A bool.</returns>
  147. public static bool GetDraggingShowValue(DependencyObject element)
  148. {
  149. return (bool)element.GetValue(DraggingShowValueProperty);
  150. }
  151. /// <summary>
  152. /// 拖拽圆示直径
  153. /// </summary>
  154. public static readonly DependencyProperty GripDiameterProperty = DependencyProperty.RegisterAttached(
  155. "GripDiameter", typeof(double), typeof(SliderHelper), new PropertyMetadata(default(double)));
  156. /// <summary>
  157. /// Sets the grip diameter.
  158. /// </summary>
  159. /// <param name="element">The element.</param>
  160. /// <param name="value">The value.</param>
  161. public static void SetGripDiameter(DependencyObject element, double value)
  162. {
  163. element.SetValue(GripDiameterProperty, value);
  164. }
  165. /// <summary>
  166. /// Gets the grip diameter.
  167. /// </summary>
  168. /// <param name="element">The element.</param>
  169. /// <returns>A double.</returns>
  170. public static double GetGripDiameter(DependencyObject element)
  171. {
  172. return (double)element.GetValue(GripDiameterProperty);
  173. }
  174. /// <summary>
  175. /// 轨道厚度
  176. /// </summary>
  177. public static readonly DependencyProperty TrackThicknessProperty = DependencyProperty.RegisterAttached(
  178. "TrackThickness", typeof(double), typeof(SliderHelper), new PropertyMetadata(default(double)));
  179. public static void SetTrackThickness(DependencyObject element, double value)
  180. {
  181. element.SetValue(TrackThicknessProperty, value);
  182. }
  183. public static double GetTrackThickness(DependencyObject element)
  184. {
  185. return (double)element.GetValue(TrackThicknessProperty);
  186. }
  187. /// <summary>
  188. /// 当前值显示字符串格式
  189. /// </summary>
  190. public static readonly DependencyProperty ValueStringFormatProperty = DependencyProperty.RegisterAttached(
  191. "ValueStringFormat", typeof(string), typeof(SliderHelper), new PropertyMetadata(default(string)));
  192. /// <summary>
  193. /// Sets the value offset.
  194. /// </summary>
  195. /// <param name="element">The element.</param>
  196. /// <param name="value">The value.</param>
  197. public static void SetValueStringFormat(DependencyObject element, string value) => element.SetValue(ValueStringFormatProperty, value);
  198. /// <summary>
  199. /// Gets the value offset.
  200. /// </summary>
  201. /// <param name="element">The element.</param>
  202. /// <returns>A double.</returns>
  203. public static string GetValueStringFormat(DependencyObject element) => (string)element.GetValue(ValueStringFormatProperty);
  204. }
  205. }