asusralis
Topic Author
Posts: 142
Joined: 30 Jul 2018, 05:03

Tooltip's Horizontal/Vertical offset seems to not work the first time the tooltip is created.

30 Nov 2020, 17:43

You can see a video of the problem here: https://files.catbox.moe/an40mv.mp4

I created a tooltip that follows the cursor. However, as you can see in the video, whenever I close and reopen the view the tooltip's position will not update at first.
private void SmartToolTip_Opened(object sender, RoutedEventArgs args)
{
        _targetElement = PlacementTarget;
        _targetElement.MouseMove += PlacementTarget_MouseMove;

        UpdatePosition(Mouse.GetPosition(_targetElement));
}

private void UpdatePosition(Point position)
{
    Point spacing = new Point(40, 40);

    Point offset = new Point(
        HorizontalPlacement == HorizontalPlacement.Left ? -ActualWidth - spacing.X : spacing.X,
        0);

    HorizontalOffset = position.X + offset.X;
    VerticalOffset = position.Y + offset.Y;
}
The Mouse.GetPosition is returning the correct position, but it seems setting the Horizontal/Vertical offset during this time does not work. Only after the mouse starts to move (which UpdatePosition is called in PlacementTarget_MouseMove) does the tooltip's position update correctly.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Tooltip's Horizontal/Vertical offset seems to not work the first time the tooltip is created.

02 Dec 2020, 12:06

One question, how are you moving up the tooltip?
I mean, in your code the VerticalOffset just gets the Y from the mouse position, because offset.Y is always 0, so the top-left corner of the tooltip should be aligned to the mouse, but in the video when the mouse is moved it then gets aligned to the bottom-left corner of the tooltip.
In case you are using ActualHeight of the tooltip to calculate an offset, I guess the problem is that during Opened event the ToolTip is not yet measured, and ActualHeight has not been calculated. You can add a SizeChanged handler and call UpdatePosition there to solve that.
 
asusralis
Topic Author
Posts: 142
Joined: 30 Jul 2018, 05:03

Re: Tooltip's Horizontal/Vertical offset seems to not work the first time the tooltip is created.

02 Dec 2020, 21:05

Ah, the size was the problem. I listened to SizeChanged & updated the position off of that, thanks.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Tooltip's Horizontal/Vertical offset seems to not work the first time the tooltip is created.

03 Dec 2020, 09:30

Great, thanks for the update.

Who is online

Users browsing this forum: Google [Bot] and 21 guests