简体   繁体   English

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

[英]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: TooltipService.ShowDuration有效,但您必须在具有Tooltip的对象上设置它,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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