C# 全局更改 ToolTip InitialShowDelay 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/952965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:08:06  来源:igfitidea点击:

Change the ToolTip InitialShowDelay Property Globally

c#wpfxamltooltip

提问by Charlie

I have an application that has upwards of a hundred different ToolTips set on a Ribbon control. All of the ToolTips pop up rather quickly (about half a second), and I would like to increase the pop up delay. After some research it appears the only way to do this in WPF is through the ToolTipService.InitialShowDelay property.

我有一个应用程序,它在功能区控件上设置了一百多个不同的工具提示。所有工具提示都弹出得相当快(大约半秒),我想增加弹出延迟。经过一些研究,似乎在 WPF 中执行此操作的唯一方法是通过 ToolTipService.InitialShowDelay 属性。

My question is, do I have to go through the XAML and explicitly say

我的问题是,我是否必须通过 XAML 并明确说

ToolTipService.InitialShowDelay="2000"

For every single control that has a ToolTip? Or is there some way to set this property globally, using something like a Style?

对于每个具有工具提示的控件?或者有什么方法可以使用样式之类的东西来全局设置此属性?

Thanks for any ideas.

感谢您的任何想法。

采纳答案by Nicholas Armstrong

Unfortunately, there's no easy way to do this. Ideally, you'd set ToolTipService.InitialShowDelay on FrameworkElement and let it propagate from there, but it turns out that doesn't seem to work.

不幸的是,没有简单的方法可以做到这一点。理想情况下,您应该在 FrameworkElement 上设置 ToolTipService.InitialShowDelay 并让它从那里传播,但事实证明这似乎不起作用。

Instead, you can set it on each typeof control you want to set it on, for instance:

相反,您可以在要设置它的每种类型的控件上设置它,例如:

<Style TargetType="RibbonButton">
    <Setter Property="ToolTipService.InitialShowDelay" Value="2000"/>
</Style>
<Style TargetType="RibbonToggleButton">
    <Setter Property="ToolTipService.InitialShowDelay" Value="2000"/>
</Style>
<Style TargetType="RibbonDropDownButton">
    <Setter Property="ToolTipService.InitialShowDelay" Value="2000"/>
</Style>

etc.

等等。

Although this is a pretty verbose way of doing it, at least you only have to set it on each type of control and not every control itself - and if you're using it in the Ribbon, then there's only a handful of controls to begin with.

尽管这是一种相当冗长的方法,但至少您只需在每种类型的控件上设置它,而不是在每个控件本身上进行设置 - 如果您在功能区中使用它,则只需少量控件即可开始和。

To save yourself some hassle should you ever want to change the value, you may want to architect the above code using a resource value:

如果您想更改该值,为了省去一些麻烦,您可能需要使用资源值来构建上述代码:

<sys:Int32 x:Key="ToolTipInitialShowDelay">2000</sys:Int32>
<Style TargetType="RibbonButton">
    <Setter Property="ToolTipService.InitialShowDelay" 
            Value="{StaticResource ToolTipInitialShowDelay}"/>
</Style>
<Style TargetType="RibbonToggleButton">
    <Setter Property="ToolTipService.InitialShowDelay" 
            Value="{StaticResource ToolTipInitialShowDelay}"/>
</Style>
<Style TargetType="RibbonDropDownButton">
    <Setter Property="ToolTipService.InitialShowDelay" 
            Value="{StaticResource ToolTipInitialShowDelay}"/>
</Style>

Alternatively, if you are not already using BasedOn styles, you could shorten it to:

或者,如果您还没有使用 BasedOn 样式,您可以将其缩短为:

<Style x:Key="ToolTipDefaults">
    <Setter Property="ToolTipService.InitialShowDelay" Value="2000"/>
</Style>
<Style TargetType="RibbonButton" BasedOn="{StaticResource ToolTipDefaults}"/>
<Style TargetType="RibbonToggleButton" BasedOn="{StaticResource ToolTipDefaults}"/>
<Style TargetType="RibbonDropDownButton" BasedOn="{StaticResource ToolTipDefaults}"/>

The limitation to this approach being that a style can only be based on one parent style, so if you're already using this pattern, you won't be able to do this.

这种方法的限制是样式只能基于一个父样式,因此如果您已经在使用此模式,则将无法执行此操作。

回答by archimed7592

I've run into the same problem and achieved an appreciable solution. Two of them, actually.

我遇到了同样的问题并获得了可观的解决方案。其中两个,实际上。

Both of them are based on DependencyProperty metadata system. For both of them you will need some pretty similar static-initialization code:

它们都基于 DependencyProperty 元数据系统。对于它们,您将需要一些非常相似的静态初始化代码:

public static class ToolTipServiceHelper
{
    static ToolTipServiceHelper()
    {
        ToolTipService.InitialShowDelayProperty
            .OverrideMetadata(typeof(FrameworkElement), 
                              new FrameworkPropertyMetadata(...));
    }
}

What goes instead of "..."?

什么代替“...”?

First solution is pretty obvious: place there the desired default value which will apply to the entire application except places where an actual value is provided.

第一个解决方案非常明显:在那里放置所需的默认值,该默认值将应用于整个应用程序,除了提供实际值的地方。

The second solution is the tricky one: instead of "..." you provide default value from default metadata, but aside from that you change the options, actually you have to make the property inheritable.

第二种解决方案很棘手:您提供默认元数据中的默认值而不是“...”,但除此之外,您还可以更改选项,实际上您必须使属性可继承。

new FrameworkPropertyMetadata(
    ToolTipService.InitialShowDelayProperty.DefaultMetadata.DefaultValue,
    FrameworkPropertyMetadataOptions.Inherits)

When the property is inheritable you can make things like this:

当属性是可继承的,你可以做这样的事情:

<Window xmlns="..."
        ...
        ToolTipService.InitialShowDelay="2000">
    ...
</Window>

That will do the trick for the entire window or any other element you apply the property to.

这将对整个窗口或您应用该属性的任何其他元素起作用。

HTH

HTH

回答by Mike Eshva

I like the solution of archimed7592 but it will not run by itself. You need to use class somehow to run it's static constructor. So I've choose to place this code into static constructor of my application Application class like this:

我喜欢 archimed7592 的解决方案,但它不会自行运行。您需要以某种方式使用类来运行它的静态构造函数。所以我选择将此代码放入我的应用程序 Application 类的静态构造函数中,如下所示:

    static NetFriendApplication()
    {
        ToolTipService.ShowDurationProperty.OverrideMetadata(
            typeof (FrameworkElement), new FrameworkPropertyMetadata(int.MaxValue));
    }

And in my case I need to set another property but idea is the same. So don't be curious.

在我的情况下,我需要设置另一个属性,但想法是一样的。所以不要好奇。

回答by maykonvs

The solution of Nicholas Armstrong is very good, but styling a FrameworkElement works if you set the ToolTipService.IsEnabled to true.

Nicholas Armstrong 的解决方案非常好,但如果您将 ToolTipService.IsEnabled 设置为 true,则对 FrameworkElement 进行样式设置即可。

In my project I have a commom style for every field and setting the ToolTip.InitialDelay there was the reasonable thing to do and it didn't work, so I gave it a try to the ToolTip.IsEnabled and it worked.

在我的项目中,我对每个字段都有一个通用样式,并设置 ToolTip.InitialDelay 有合理的事情要做,但它不起作用,所以我尝试了 ToolTip.IsEnabled 并且它起作用了。

Example below:

下面的例子:

<Style x:Key="FieldStyle" TargetType="FrameworkElement">
    <Setter Property="ToolTipService.ShowOnDisabled" Value="True"/>
    <Setter Property="ToolTipService.ShowDuration" Value="20000"/>
    <Setter Property="ToolTipService.InitialShowDelay" Value="3000"/>
    <Setter Property="ToolTipService.IsEnabled" Value="True"/>
</Style>