Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

c# - Change the ToolTip InitialShowDelay Property Globally

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...