You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ResourceDictionary中设置ToolTipService.InitialShowDelay的默认值

解决ToolTipService.InitialShowDelay绑定资源报错问题

问题场景

希望在ResourceDictionary中统一设置ToolTipService.InitialShowDelay的默认值,原元素直接写固定值500可正常运行,但绑定动态资源后触发错误:

System.InvalidOperationException: ''500' is not a valid value for property 'InitialShowDelay'.'

原元素代码:

<Border x:Name="VersionBox_Border"
        Grid.Column="0"
        ToolTipService.InitialShowDelay="500">
    <Border.ToolTip>
        <ToolTip Content="..."/>
    </Border.ToolTip>
</Border>

错误的ResourceDictionary定义:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:Double x:Key="Common_ToolTip_msTime">500</system:Double>

</ResourceDictionary>

错误原因

ToolTipService.InitialShowDelay的属性类型为int(整数),但资源中定义的是system:Double类型,类型不匹配导致无法完成转换,从而抛出异常。

解决方案

1. 修正资源类型

将ResourceDictionary中的资源类型改为system:Int32,匹配属性要求的整数类型:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:Int32 x:Key="Common_ToolTip_msTime">500</system:Int32>

</ResourceDictionary>

元素代码保持绑定写法即可:

<Border x:Name="VersionBox_Border"
        Grid.Column="0"
        ToolTipService.InitialShowDelay="{DynamicResource Common_ToolTip_msTime}">
    <Border.ToolTip>
        <ToolTip Content="..."/>
    </Border.ToolTip>
</Border>

2. 全局样式统一设置(更推荐)

如果需要给所有控件统一设置ToolTip初始延迟,可以定义全局Style,无需逐个元素绑定:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:Int32 x:Key="Common_ToolTip_msTime">500</system:Int32>

    <Style TargetType="FrameworkElement">
        <Setter Property="ToolTipService.InitialShowDelay" Value="{StaticResource Common_ToolTip_msTime}"/>
    </Style>
</ResourceDictionary>

所有继承自FrameworkElement的控件会自动应用该延迟值,实现全局统一配置。

内容的提问来源于stack exchange,提问作者Simos Sigma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 02:48:31