Forcing a WPF tooltip to stay on the screen

2019-01-13 23:53发布

I have a tooltip for a Label and I want it to stay open until the user moves the mouse to a different control.

I have tried the following properties on the tooltip:

StaysOpen="True"

and

TooltipService.ShowDuration = "60000"

But in both cases the tooltip is only displayed for exactly 5 seconds.

Why are these values being ignored?

10条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-14 00:09

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

<Label ToolTipService.ShowDuration="12000" Name="lblShowTooltip" Content="Shows tooltip">
    <Label.ToolTip>
        <ToolTip>
            <TextBlock>Hello world!</TextBlock>
        </ToolTip>
    </Label.ToolTip>
</Label>

I'd say that this design was chosen because it allows same tooltip with different timeouts on different controls.

查看更多
Fickle 薄情
3楼-- · 2019-01-14 00:09

Got my issue fixed with the same code.

ToolTipService.ShowDurationProperty.OverrideMetadata( typeof(DependencyObject), new FrameworkPropertyMetadata(Int32.MaxValue));

查看更多
孤傲高冷的网名
4楼-- · 2019-01-14 00:12

You probably want to use Popup instead of Tooltip, since Tooltip assumes that you're using it in the pre-defined UI-standards way.

I'm not sure why StaysOpen doesn't work, but ShowDuration works as documented in MSDN -- it's the amount of time the Tooltip is displayed WHEN it's displayed. Set it to a small amount (e.g. 500 msec) to see the difference.

The trick in your case is maintaining the "last hovered control" state, but once you have that it should be fairly trivial to change the placement target and the content dynamically (either manually, or via binding) if you're using one Popup, or hiding the last visible Popup if you're using multiple.

There are some gotchas with Popups as far as Window resizing and moving (Popups don't move w/the containers), so you may want to also have that in mind while you're tweaking the behavior. See this link for more details.

HTH.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-14 00:17
ToolTipService.ShowDurationProperty.OverrideMetadata(
    typeof(DependencyObject), new FrameworkPropertyMetadata(Int32.MaxValue));

It is working for me. Copy this line into your class constructor.

查看更多
登录 后发表回答