如何设置工具提示样式的InitialShowDelay?

时间:2020-10-13 11:40:31

标签: wpf wpf-style

我正在尝试为工具提示设置样式的延迟。为此,我使用了依赖项属性。

这是样式:

<Style TargetType="ToolTip" x:Key="ToolTipDefaultStyle">
    <Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
    <Setter Property="ToolTipService.InitialShowDelay" Value="{Binding PlacementTarget.(ap:ToolTipAttachedProperty.InitialShowDelay), RelativeSource={RelativeSource AncestorType=ToolTip}}"/>
    <Setter Property="ToolTipService.ShowDuration" Value="{Binding PlacementTarget.(ap:ToolTipAttachedProperty.ShowDuration), RelativeSource={RelativeSource AncestorType=ToolTip}}"/>
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding PlacementTarget.(ap:ToolTipAttachedProperty.Texto), RelativeSource={RelativeSource AncestorType=ToolTip}}"  MaxWidth="400" TextWrapping='Wrap' />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是视图:

<StackPanel Name="spTiposIva" Orientation="Vertical" Margin="5,0,0,0"
            ap:ToolTipAttachedProperty.Texto="{Binding TiposIvaTooltip}"
            ap:ToolTipAttachedProperty.InitialShowDelay="10000"
            ap:ToolTipAttachedProperty.ShowDuration="{StaticResource TooltipDisplayTime}">
    <StackPanel.ToolTip>
        <ToolTip Style="{StaticResource ToolTipDefaultStyle}"/>
    </StackPanel.ToolTip>
</StackPanel>

可以正确显示文本,但初始显示延迟是默认值,不会延迟10秒。

如何设置延迟时间?

谢谢。

1 个答案:

答案 0 :(得分:1)

其他属性,例如ShowOnDisabled也不起作用。这是因为您在ToolTipService本身上设置了附加的ToolTip属性,或者将其设置为定位样式。相反,您必须在工具提示所关联的控件上设置这些属性,而不是不是工具提示本身

如果您将它们直接附加到StackPanel或采用某种样式,例如,

<StackPanel Name="spTiposIva" Orientation="Vertical" Margin="5,0,0,0"
            ToolTipService.ShowOnDisabled="True"
            ToolTipService.InitialShowDelay="10000"
            ToolTipService.ShowDuration="{StaticResource TooltipDisplayTime}"
            ToolTip="Binding TiposIvaTooltip">
   <!-- ...your content. -->
</StackPanel>

您可以直接将工具提示服务属性直接附加到控件。您无需为ToolTipService已经提供的相同内容创建自己的附加属性。看起来好像是间接的,但是您可以做到。在下面,您可以找到有效样式的示例。

在上面的示例中,您可以拆分ToolTip样式并为StackPanel创建样式。

<Style TargetType="{x:Type StackPanel}" x:Key="StackPanelDefaultStyle">
   <Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
   <Setter Property="ToolTipService.InitialShowDelay" Value="{Binding (ap:ToolTipAttachedProperty.InitialShowDelay), RelativeSource={RelativeSource Self}}"/>
   <Setter Property="ToolTipService.ShowDuration" Value="{Binding (ap:ToolTipAttachedProperty.ShowDuration), RelativeSource={RelativeSource Self}}"/>
</Style>
<Style TargetType="ToolTip" x:Key="ToolTipDefaultStyle">
   <Setter Property="ContentTemplate">
      <Setter.Value>
         <DataTemplate>
            <StackPanel>
               <TextBlock Text="{Binding PlacementTarget.(ap:ToolTipAttachedProperty.Texto), RelativeSource={RelativeSource AncestorType=ToolTip}}"  MaxWidth="400" TextWrapping='Wrap' />
            </StackPanel>
         </DataTemplate>
      </Setter.Value>
   </Setter>
</Style>

另一种方法可能是创建其他控件样式所基于的基本样式。它与StackPanel的样式相同,但是将FrameworkElement作为目标类型应用于所有控件。

<Style TargetType="{x:Type FrameworkElement}" x:Key="FrameworkElementDefaultStyle">