如何在WPF中强制显示工具提示

时间:2021-11-07 19:28:14

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?

有没有办法强制显示工具提示?

2 个解决方案

#1


6  

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;

#2


3  

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

TooltipService.ShowDuration有效,但您必须在具有Tooltip的对象上设置它,如下所示:

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

#1


6  

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;

#2


3  

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

TooltipService.ShowDuration有效,但您必须在具有Tooltip的对象上设置它,如下所示:

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