Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

105 rindas
3.4 KiB

  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Data;
  4. using System.Windows.Documents;
  5. using System.Windows.Media;
  6. namespace BPA.UIControl
  7. {
  8. /// <summary>
  9. /// Slider 当前值装饰器
  10. /// </summary>
  11. public class SliderValueAdorner : Adorner
  12. {
  13. private readonly TextBlock valueTextBlock;
  14. private readonly Card card;
  15. private readonly Dock placement;
  16. private readonly double offset;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="SliderValueAdorner"/> class.
  19. /// </summary>
  20. /// <param name="adornedElement">添加装饰器控件</param>
  21. /// <param name="slider">Slider 控件</param>
  22. /// <param name="placement">显示位置</param>
  23. /// <param name="offset">偏移</param>
  24. /// <param name="stringFormat">字符串格式</param>
  25. public SliderValueAdorner(UIElement adornedElement, Slider slider, Dock placement, double offset, string stringFormat)
  26. : base(adornedElement)
  27. {
  28. this.placement = placement;
  29. this.offset = offset;
  30. var binding = new Binding
  31. {
  32. Source = slider,
  33. Path = new PropertyPath("Value"),
  34. StringFormat = stringFormat
  35. };
  36. valueTextBlock = new TextBlock
  37. {
  38. FontSize = 13,
  39. Foreground = (Brush)Application.Current.FindResource("DefaultBackground")
  40. };
  41. valueTextBlock.SetBinding(TextBlock.TextProperty, binding);
  42. card = new Card
  43. {
  44. Background = (Brush)Application.Current.FindResource("DefaultForeground"),
  45. Padding = new Thickness(10, 5, 10, 5),
  46. Content = valueTextBlock,
  47. };
  48. }
  49. /// <inheritdoc/>
  50. protected override Visual GetVisualChild(int index) => card;
  51. /// <inheritdoc/>
  52. protected override int VisualChildrenCount => 1;
  53. /// <inheritdoc/>
  54. protected override Size MeasureOverride(Size constraint)
  55. {
  56. card.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  57. return card.DesiredSize;
  58. }
  59. /// <inheritdoc/>
  60. protected override Size ArrangeOverride(Size finalSize)
  61. {
  62. var cardSize = card.DesiredSize;
  63. var elementSize = AdornedElement.RenderSize;
  64. double x = 0;
  65. double y = 0;
  66. switch (placement)
  67. {
  68. case Dock.Left:
  69. x = cardSize.Width * -1 + offset;
  70. y = cardSize.Height / -2 + elementSize.Height / 2;
  71. break;
  72. case Dock.Top:
  73. x = cardSize.Width / -2 + elementSize.Width / 2;
  74. y = cardSize.Height * -1 + offset;
  75. break;
  76. case Dock.Right:
  77. x = elementSize.Width + offset;
  78. y = cardSize.Height / -2 + elementSize.Height / 2;
  79. break;
  80. case Dock.Bottom:
  81. x = cardSize.Width / -2 + elementSize.Width / 2;
  82. y = elementSize.Height + offset;
  83. break;
  84. }
  85. var rect = new Rect(new Point(x, y), cardSize);
  86. card.Arrange(rect);
  87. return base.ArrangeOverride(finalSize);
  88. }
  89. }
  90. }