Change the ToolTip InitialShowDelay Property Globa

2019-01-23 13:03发布

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.

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

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.

4条回答
看我几分像从前
2楼-- · 2019-01-23 13:43

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:

    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.

查看更多
我命由我不由天
3楼-- · 2019-01-23 13:50

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:

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

查看更多
干净又极端
4楼-- · 2019-01-23 13:51

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.

Instead, you can set it on each type of 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:

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

查看更多
对你真心纯属浪费
5楼-- · 2019-01-23 14:01

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

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.

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>
查看更多
登录 后发表回答