Forcing a WPF tooltip to stay on the screen

前端 未结 10 1946
别那么骄傲
别那么骄傲 2020-12-08 18:24

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:<

相关标签:
10条回答
  • 2020-12-08 18:43

    I was wrestling with the WPF Tooltip only the other day. It doesn't seem to be possible to stop it from appearing and disappearing by itself, so in the end I resorted to handling the Opened event. For example, I wanted to stop it from opening unless it had some content, so I handled the Opened event and then did this:

    tooltip.IsOpen = (tooltip.Content != null);
    

    It's a hack, but it worked.

    Presumably you could similarly handle the Closed event and tell it to open again, thus keeping it visible.

    0 讨论(0)
  • 2020-12-08 18:45

    Also if you ever want to put any other control in your ToolTip, it won't be focusable since a ToolTip itself can get focus. So Like micahtan said, your best shot is a Popup.

    0 讨论(0)
  • 2020-12-08 18:46
    ToolTipService.ShowDurationProperty.OverrideMetadata(
        typeof(DependencyObject), new FrameworkPropertyMetadata(Int32.MaxValue));
    

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

    0 讨论(0)
  • 2020-12-08 18:49

    If you want to specify that only certain elements in your Window have effectively indefinite ToolTip duration you can define a Style in your Window.Resources for those elements. Here is a Style for Button that has such a ToolTip :

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        ...>
        ...
        <Window.Resources>
            <Style x:Key="ButtonToolTipIndefinate" TargetType="{x:Type Button}">
                <Setter Property="ToolTipService.ShowDuration"
                        Value="{x:Static Member=sys:Int32.MaxValue}"/>
            </Style>
            ...
        </Window.Resources>
        ...
        <Button Style="{DynamicResource ButtonToolTipIndefinate}"
                ToolTip="This should stay open"/>
        <Button ToolTip="This Should disappear after the default time.">
        ...
    

    One can also add Style.Resources to the Style to change the appearance of the ToolTip it shows, for example:

    <Style x:Key="ButtonToolTipTransparentIndefinate" TargetType="{x:Type Button}">
        <Style.Resources>
            <Style x:Key="{x:Type ToolTip}" TargetType="{x:Type ToolTip}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="HasDropShadow" Value="False"/>
            </Style>
        </Style.Resources>
        <Setter Property="ToolTipService.ShowDuration"
                Value="{x:Static Member=sys:Int32.MaxValue}"/>
    </Style>
    

    Note: When I did this I also used BasedOn in the Style so everything else defined for the version of my custom control with a normal ToolTip would be applied.

    0 讨论(0)
提交回复
热议问题