BlackToolTip.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Controls.Primitives;
  4. using System.Windows.Media;
  5. namespace aipdev
  6. {
  7. public enum PlacementDirection
  8. {
  9. Left,
  10. Top,
  11. Right,
  12. Bottom
  13. }
  14. class BlackToolTip:ToolTip
  15. {
  16. public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
  17. "Text", typeof(string), typeof(BlackToolTip), new PropertyMetadata(null));
  18. public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register(
  19. "Direction", typeof(PlacementDirection), typeof(BlackToolTip), new PropertyMetadata(null));
  20. private readonly Border _border;
  21. private readonly TextBlock _textBlock;
  22. public string Text
  23. {
  24. get => (string)GetValue(TextProperty);
  25. set => SetValue(TextProperty, value);
  26. }
  27. public PlacementDirection Direction
  28. {
  29. get => (PlacementDirection)GetValue(DirectionProperty);
  30. set => SetValue(DirectionProperty, value);
  31. }
  32. public BlackToolTip()
  33. {
  34. BorderThickness = new Thickness(0);
  35. Padding = new Thickness(0);
  36. Background = Brushes.Transparent;
  37. Foreground = Brushes.Transparent;
  38. _border = new Border
  39. {
  40. Background = Brushes.Black,
  41. BorderBrush = Brushes.Gray,
  42. BorderThickness = new Thickness(0.5),
  43. CornerRadius = new CornerRadius(4.0),
  44. };
  45. _textBlock = new TextBlock
  46. {
  47. VerticalAlignment = VerticalAlignment.Center,
  48. HorizontalAlignment = HorizontalAlignment.Center,
  49. Background = Brushes.Transparent,
  50. Foreground = Brushes.White,
  51. FontSize = 14,
  52. Margin = new Thickness(8),
  53. TextWrapping= TextWrapping.Wrap
  54. };
  55. _border.Child = _textBlock;
  56. Content = _border;
  57. }
  58. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  59. {
  60. UpdateOffset();
  61. base.OnRenderSizeChanged(sizeInfo);
  62. }
  63. private void UpdateOffset()
  64. {
  65. if (PlacementTarget is FrameworkElement element)
  66. {
  67. if (Direction == PlacementDirection.Right || Direction == PlacementDirection.Left)
  68. {
  69. var elementHeight = element.ActualHeight;
  70. var height = ActualHeight;
  71. var offset = (elementHeight - height) / 2;
  72. VerticalOffset = offset;
  73. }
  74. else if (Direction == PlacementDirection.Top || Direction == PlacementDirection.Bottom)
  75. {
  76. var elementWidth = element.ActualWidth;
  77. var width = ActualWidth;
  78. var offset = (elementWidth - width) / 2;
  79. HorizontalOffset = offset;
  80. }
  81. }
  82. }
  83. protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  84. {
  85. base.OnPropertyChanged(e);
  86. if (e.Property == TextProperty)
  87. {
  88. var text = (string) e.NewValue;
  89. _textBlock.Text = text;
  90. }
  91. if (e.Property == DirectionProperty)
  92. {
  93. var direction = (PlacementDirection) e.NewValue;
  94. if (direction == PlacementDirection.Left)
  95. {
  96. Placement = PlacementMode.Left;
  97. }
  98. if (direction == PlacementDirection.Top)
  99. {
  100. Placement = PlacementMode.Top;
  101. }
  102. if (direction == PlacementDirection.Right)
  103. {
  104. Placement = PlacementMode.Right;
  105. }
  106. if (direction == PlacementDirection.Bottom)
  107. {
  108. Placement = PlacementMode.Bottom;
  109. }
  110. }
  111. }
  112. }
  113. }