using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; namespace aipdev { public enum PlacementDirection { Left, Top, Right, Bottom } class BlackToolTip:ToolTip { public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(BlackToolTip), new PropertyMetadata(null)); public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register( "Direction", typeof(PlacementDirection), typeof(BlackToolTip), new PropertyMetadata(null)); private readonly Border _border; private readonly TextBlock _textBlock; public string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); } public PlacementDirection Direction { get => (PlacementDirection)GetValue(DirectionProperty); set => SetValue(DirectionProperty, value); } public BlackToolTip() { BorderThickness = new Thickness(0); Padding = new Thickness(0); Background = Brushes.Transparent; Foreground = Brushes.Transparent; _border = new Border { Background = Brushes.Black, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0.5), CornerRadius = new CornerRadius(4.0), }; _textBlock = new TextBlock { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, Background = Brushes.Transparent, Foreground = Brushes.White, FontSize = 14, Margin = new Thickness(8), TextWrapping= TextWrapping.Wrap }; _border.Child = _textBlock; Content = _border; } protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) { UpdateOffset(); base.OnRenderSizeChanged(sizeInfo); } private void UpdateOffset() { if (PlacementTarget is FrameworkElement element) { if (Direction == PlacementDirection.Right || Direction == PlacementDirection.Left) { var elementHeight = element.ActualHeight; var height = ActualHeight; var offset = (elementHeight - height) / 2; VerticalOffset = offset; } else if (Direction == PlacementDirection.Top || Direction == PlacementDirection.Bottom) { var elementWidth = element.ActualWidth; var width = ActualWidth; var offset = (elementWidth - width) / 2; HorizontalOffset = offset; } } } protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); if (e.Property == TextProperty) { var text = (string) e.NewValue; _textBlock.Text = text; } if (e.Property == DirectionProperty) { var direction = (PlacementDirection) e.NewValue; if (direction == PlacementDirection.Left) { Placement = PlacementMode.Left; } if (direction == PlacementDirection.Top) { Placement = PlacementMode.Top; } if (direction == PlacementDirection.Right) { Placement = PlacementMode.Right; } if (direction == PlacementDirection.Bottom) { Placement = PlacementMode.Bottom; } } } } }