Good day. I mess around with my own ToolTip presentation. The question is as follows. There is a ToolTip whose Placement property is set to Right. If the element on which ToolTip is displayed is not close to the right edge of the screen and there is enough space to display it, then ToolTip is displayed correctly, if it is not enough, then ToolTip is displayed as if its Placement property is set to Left. Tell me how you can find out which side the ToolTip is currently displayed.
1 answer
Probably found a solution, but it is not as elegant as we would like. Actually, there are two possible solutions:
For ToolTip, set the Placement property to Custom. Next, attach the delegate to the ToolTip CustomPopupPlacementCallback property:
CustomPopupPlacement[] placeToolTip(Size popupSize, Size targetSize, Point offset)in which to describe the display options ToolTip.
The second option is to define the placement of the ToolTip relative to its parent:
private void ToolTip_IsVisibleChanged(object sender, RoutedEventArgs e) { if ((bool)e.NewValue) { Point p = (sender as ToolTip).PointToScreen(new Point()); Point t = ((sender as ToolTip).PlacementTarget).PointToScreen(new Point()); if ((sender as ToolTip).Placement == PlacementMode.Right || (sender as ToolTip).Placement == PlacementMode.Left) { if (pX > tX) (sender as ToolTip).Placement = PlacementMode.Right; else if (pX < tX) (sender as ToolTip).Placement = PlacementMode.Left; } if ((sender as ToolTip).Placement == PlacementMode.Bottom || (sender as ToolTip).Placement == PlacementMode.Top) { if (pY > tY) (sender as ToolTip).Placement = PlacementMode.Top; else if (pY < tY) (sender as ToolTip).Placement = PlacementMode.Bottom; } } }
Both options work, that's just as mentioned above, both methods are not particularly elegant.
- I would cache
sender as ToolTipinto a variable. - VladD - @VladD, thank you for your comment. This is just a sketch, which shows the work of the solution - Tom Dugger
|