全局更改ToolTip InitialShowDelay属性

Cha*_*lie 19 c# wpf xaml tooltip

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

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

ToolTipService.InitialShowDelay="2000"
Run Code Online (Sandbox Code Playgroud)

对于每个具有ToolTip的控件?或者是否有某种方法可以使用类似Style的方式全局设置此属性?

谢谢你的任何想法.

Nic*_*ong 27

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

相反,你可以将它设置在各类型要设置上,比如控制:

<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>
Run Code Online (Sandbox Code Playgroud)

等等

虽然这是一种非常冗长的方式,但至少你只需要在每种控件上设置它而不是每个控件本身 - 如果你在Ribbon中使用它,那么只有少数几个控件可以开始用.

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

<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>
Run Code Online (Sandbox Code Playgroud)

或者,如果您尚未使用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}"/>
Run Code Online (Sandbox Code Playgroud)

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


arc*_*592 12

我遇到了同样的问题并取得了明显的解决方案.其中两个,实际上.

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

public static class ToolTipServiceHelper
{
    static ToolTipServiceHelper()
    {
        ToolTipService.InitialShowDelayProperty
            .OverrideMetadata(typeof(FrameworkElement), 
                              new FrameworkPropertyMetadata(...));
    }
}
Run Code Online (Sandbox Code Playgroud)

是什么而不是"......"?

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

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

new FrameworkPropertyMetadata(
    ToolTipService.InitialShowDelayProperty.DefaultMetadata.DefaultValue,
    FrameworkPropertyMetadataOptions.Inherits)
Run Code Online (Sandbox Code Playgroud)

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

<Window xmlns="..."
        ...
        ToolTipService.InitialShowDelay="2000">
    ...
</Window>
Run Code Online (Sandbox Code Playgroud)

这将完成整个窗口或您应用该属性的任何其他元素的技巧.

HTH


Mik*_*hva 8

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

    static NetFriendApplication()
    {
        ToolTipService.ShowDurationProperty.OverrideMetadata(
            typeof (FrameworkElement), new FrameworkPropertyMetadata(int.MaxValue));
    }
Run Code Online (Sandbox Code Playgroud)

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