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.
我有一个应用程序,它在Ribbon控件上设置了100多个不同的工具提示。所有的工具提示都会快速弹出(大约半秒),我想增加弹出延迟。在一些研究之后,在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.
谢谢你的任何想法。
4 个解决方案
#1
24
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.
不幸的是,没有简单的方法可以做到这一点。理想情况下,你会设置ToolTipService。InitialShowDelay在FrameworkElement上执行,然后让它从那里传播出去,但结果似乎并不奏效。
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:
或者,如果您还没有使用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.
这种方法的局限性是,一个样式只能基于一个父样式,因此如果您已经使用了这个模式,您将无法做到这一点。
#2
12
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
HTH
#3
7
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的解决方案,但它不会自己运行。您需要使用类来运行它的静态构造函数。因此,我选择将这段代码放在应用程序类的静态构造函数中,如下所示:
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.
在我的例子中,我需要设置另一个属性但是想法是一样的。所以不要好奇。
#4
3
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。
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.
在我的项目中,我对每个字段都有一个通用的样式并设置工具提示。初始延迟有一个合理的事情要做,但是它不起作用,所以我尝试了一下工具提示。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>
#1
24
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.
不幸的是,没有简单的方法可以做到这一点。理想情况下,你会设置ToolTipService。InitialShowDelay在FrameworkElement上执行,然后让它从那里传播出去,但结果似乎并不奏效。
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:
或者,如果您还没有使用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.
这种方法的局限性是,一个样式只能基于一个父样式,因此如果您已经使用了这个模式,您将无法做到这一点。
#2
12
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
HTH
#3
7
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的解决方案,但它不会自己运行。您需要使用类来运行它的静态构造函数。因此,我选择将这段代码放在应用程序类的静态构造函数中,如下所示:
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.
在我的例子中,我需要设置另一个属性但是想法是一样的。所以不要好奇。
#4
3
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。
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.
在我的项目中,我对每个字段都有一个通用的样式并设置工具提示。初始延迟有一个合理的事情要做,但是它不起作用,所以我尝试了一下工具提示。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>