强制WPF工具提示保留在屏幕上

时间:2009-05-22 06:27:22

标签: c# wpf xaml tooltip

我有一个标签的工具提示,我希望它保持开放直到用户 将鼠标移动到不同的控件。

我在工具提示上尝试了以下属性:

StaysOpen="True"

ToolTipService.ShowDuration = "60000"

但在这两种情况下,工具提示仅显示5秒钟。

为什么忽略这些值?

10 个答案:

答案 0 :(得分:166)

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

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

我会说这个设计之所以被选中,是因为它允许在不同的控件上使用不同超时的相同工具提示。

答案 1 :(得分:95)

只需将此代码放入初始化部分即可。

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

答案 2 :(得分:14)

这也让我今晚疯狂。我创建了一个ToolTip子类来处理这个问题。对我来说,在.NET 4.0上,ToolTip.StaysOpen属性并非“真正”保持打开状态。

在下面的课程中,使用新属性ToolTipEx.IsReallyOpen,而不是属性ToolTip.IsOpen。你会得到你想要的控制。通过Debug.Print()调用,您可以在调试器“输出”窗口中查看调用this.IsOpen = false的次数! StaysOpen那么多,或者我应该说"StaysOpen"?享受。

public class ToolTipEx : ToolTip
{
    static ToolTipEx()
    {
        IsReallyOpenProperty =
            DependencyProperty.Register(
                "IsReallyOpen",
                typeof(bool),
                typeof(ToolTipEx),
                new FrameworkPropertyMetadata(
                    defaultValue: false,
                    flags: FrameworkPropertyMetadataOptions.None,
                    propertyChangedCallback: StaticOnIsReallyOpenedChanged));
    }

    public static readonly DependencyProperty IsReallyOpenProperty;

    protected static void StaticOnIsReallyOpenedChanged(
        DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        ToolTipEx self = (ToolTipEx)o;
        self.OnIsReallyOpenedChanged((bool)e.OldValue, (bool)e.NewValue);
    }

    protected void OnIsReallyOpenedChanged(bool oldValue, bool newValue)
    {
        this.IsOpen = newValue;
    }

    public bool IsReallyOpen
    {
        get
        {
            bool b = (bool)this.GetValue(IsReallyOpenProperty);
            return b;
        }
        set { this.SetValue(IsReallyOpenProperty, value); }
    }

    protected override void OnClosed(RoutedEventArgs e)
    {
        System.Diagnostics.Debug.Print(String.Format(
            "OnClosed: IsReallyOpen: {0}, StaysOpen: {1}", this.IsReallyOpen, this.StaysOpen));
        if (this.IsReallyOpen && this.StaysOpen)
        {
            e.Handled = true;
            // We cannot set this.IsOpen directly here.  Instead, send an event asynchronously.
            // DispatcherPriority.Send is the highest priority possible.
            Dispatcher.CurrentDispatcher.BeginInvoke(
                (Action)(() => this.IsOpen = true),
                DispatcherPriority.Send);
        }
        else
        {
            base.OnClosed(e);
        }
    }
}

小咆哮:为什么Microsoft没有使DependencyProperty属性(getter / setter)成为虚拟,所以我们可以接受/拒绝/调整子类中的更改?或者为每个virtual OnXYZPropertyChanged制作一个DependencyProperty?啊。

--- ---编辑

上面的解决方案在XAML编辑器中看起来很奇怪 - 工具提示始终显示,阻止Visual Studio中的某些文本!

以下是解决此问题的更好方法:

一些XAML:

<!-- Need to add this at top of your XAML file:
     xmlns:System="clr-namespace:System;assembly=mscorlib"
-->
<ToolTip StaysOpen="True" Placement="Bottom" HorizontalOffset="10"
        ToolTipService.InitialShowDelay="0" ToolTipService.BetweenShowDelay="0"
        ToolTipService.ShowDuration="{x:Static Member=System:Int32.MaxValue}"
>This is my tooltip text.</ToolTip>

一些代码:

// Alternatively, you can attach an event listener to FrameworkElement.Loaded
public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    // Be gentle here: If someone creates a (future) subclass or changes your control template,
    // you might not have tooltip anymore.
    ToolTip toolTip = this.ToolTip as ToolTip;
    if (null != toolTip)
    {
        // If I don't set this explicitly, placement is strange.
        toolTip.PlacementTarget = this;
        toolTip.Closed += new RoutedEventHandler(OnToolTipClosed);
    }
}

protected void OnToolTipClosed(object sender, RoutedEventArgs e)
{
    // You may want to add additional focus-related tests here.
    if (this.IsKeyboardFocusWithin)
    {
        // We cannot set this.IsOpen directly here.  Instead, send an event asynchronously.
        // DispatcherPriority.Send is the highest priority possible.
        Dispatcher.CurrentDispatcher.BeginInvoke(
            (Action)delegate
                {
                    // Again: Be gentle when using this.ToolTip.
                    ToolTip toolTip = this.ToolTip as ToolTip;
                    if (null != toolTip)
                    {
                        toolTip.IsOpen = true;
                    }
                },
            DispatcherPriority.Send);
    }
}

结论:类ToolTipContextMenu有所不同。两者都有“服务”类,如ToolTipServiceContextMenuService,它们管理某些属性,并且在显示期间都使用Popup作为“秘密”父控件。最后,我注意到 ALL Web上的XAML ToolTip示例不直接使用类ToolTip。相反,他们使用StackPanel嵌入TextBlock。让你说的话:“嗯...”

答案 3 :(得分:8)

您可能希望使用Popup而不是Tooltip,因为Tooltip假定您以预定义的UI标准方式使用它。

我不确定为什么StaysOpen不起作用,但ShowDuration的工作原理如MSDN中所述 - 它是工具提示显示时的显示时间。将其设置为少量(例如500毫秒)以查看差异。

你的案例中的诀窍是保持“最后一个悬停的控制”状态,但是一旦你有了这个状态,如果你正在使用它,动态地(手动或通过绑定)更改放置目标和内容应该是相当简单的一个弹出窗口,或者如果您使用多个弹出窗口,则隐藏最后一个可见的弹出窗口。

就窗口大小调整和移动而言,弹出窗口有一些问题(弹出窗口不会随容器移动),因此您可能还想在调整行为时考虑到这一点。有关详细信息,请参阅this link

HTH。

答案 4 :(得分:7)

如果您只想指定Window中的某些元素 有效地无限期ToolTip,您可以在Style中为这些元素定义Window.Resources。以下为Style的{​​{1}},其中包含Button

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."> ... 添加到Style.Resources以更改其显示的Style的外观,例如:

ToolTip

注意:当我执行此操作时,我还在<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> 中使用了BasedOn,因此将应用为我的自定义控件版本定义的所有其他内容均为Style

答案 5 :(得分:5)

前几天我正在与WPF工具提示搏斗。似乎不可能阻止它自己出现和消失,所以最后我采取了处理Opened事件。例如,我想阻止它打开,除非它有一些内容,所以我处理了Opened事件然后做了这个:

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

这是一个黑客,但它确实有效。

据推测,您可以类似地处理Closed事件并告诉它再次打开,从而使其可见。

答案 6 :(得分:2)

仅为了完整性: 在代码中它看起来像这样:

ToolTipService.SetShowDuration(element, 60000);

答案 7 :(得分:0)

此外,如果你想在你的工具提示中放入任何其他控件,它将无法聚焦,因为工具提示本身可以获得焦点。所以像micahtan说的那样,你最好的镜头就是Popup。

答案 8 :(得分:0)

使用相同的代码解决了我的问题。

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

答案 9 :(得分:-4)

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

这对我有用。将此行复制到类构造函数中。

相关问题