简体   繁体   中英

How do I force showing a tooltip in WPF

I'd like to show a tooltip when I move the mouse. Here is my code:

 private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            Grid grid = (Grid) sender;
            if (e.GetPosition(grid).X < 100)
                grid.ToolTip = e.GetPosition(grid).X.ToString();
            else
                grid.ToolTip = null;
        }

However, the tooltip disappears after I click on the grid.

Is there a way to force showing the tooltip?

var oldTT = SomeElement.ToolTip as ToolTip;
if (oldTT != null) oldTT.IsOpen = false;
SomeElement.ToolTip = new ToolTip
{
     Content = "Lalalalala",
    IsOpen = true,
};

or

var tt = SomeElement.ToolTip as ToolTip;
if (tt != null) tt.IsOpen = true;

TooltipService.ShowDuration works, but you must set it on the object having the Tooltip, like this:

   <Label ToolTipService.ShowDuration="120000" Name="lblTooltip"  Content="Shows tooltip">
<Label.ToolTip>
    <ToolTip>
        <TextBlock>Hi world!</TextBlock>
    </ToolTip>
</Label.ToolTip>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM