An style for a tooltip only works partially

Viewed 50

I would like to have an standar tooltip for all the controls, setting the initial delay, the show delay and that it can be shown when is not enabled. In the control I only would need to set the text of the tooltip in each case.

I have this style:

<sys:Int32 x:Key="TooltipInitialShowDelay">2000</sys:Int32>
<sys:Int32 x:Key="TooltipInitialShowDelayLong">5000</sys:Int32>
<sys:Int32 x:Key="TooltipDisplayTime">60000</sys:Int32>



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

I have this dependency properties:

public static class ToolTipDependencyProperty
{
    #region Texto
    public static readonly DependencyProperty TextoProperty =
        DependencyProperty.RegisterAttached(
        "Texto",
        typeof(string),
        typeof(ToolTipDependencyProperty));

    public static string GetTexto(DependencyObject obj)
    {
        return (string)obj.GetValue(TextoProperty);
    }

    public static void SetTexto(DependencyObject obj, string value)
    {
        obj.SetValue(TextoProperty, value);
    }
    #endregion Texto


    #region InitialShowDelay
    public static readonly DependencyProperty InitialShowDelayProperty =
        DependencyProperty.RegisterAttached(
        "InitialShowDelay",
        typeof(int),
        typeof(ToolTipDependencyProperty));

    public static int GetInitialShowDelay(DependencyObject obj)
    {
        return (int)obj.GetValue(InitialShowDelayProperty);
    }

    public static void SetInitialShowDelay(DependencyObject obj, int value)
    {
        obj.SetValue(InitialShowDelayProperty, value);
    }
    #endregion InitialShowDelay


    #region ShowDuration
    public static readonly DependencyProperty ShowDurationProperty =
        DependencyProperty.RegisterAttached(
        "ShowDuration",
        typeof(int),
        typeof(ToolTipDependencyProperty));

    public static int GetShowDuration(DependencyObject obj)
    {
        return (int)obj.GetValue(ShowDurationProperty);
    }

    public static void SetShowDuration(DependencyObject obj, int value)
    {
        obj.SetValue(ShowDurationProperty, value);
    }
    #endregion ShowDuration
}

And this is my button:

<Button Name="btnBuscar" Content="Buscar" Command="{Binding BuscarCommand}"
        IsEnabled="{Binding BuscarIsEnabled}"
        dp:ToolTipDependencyProperty.Texto="{Binding BuscarToolTip}">

    <Button.ToolTip>
        <ToolTip Style="{StaticResource ToolTipDefaultStyle}"/>
    </Button.ToolTip>
</Button>

The problem is that the text is ahown, but only when the button is enabled. And also the initial show delay and show duration is the standard duration, not the values that I want to use.

However, if in the control I set the ToolTip.Service variables, it works.

How could I set the duration in the style?

Thanks.

1 Answers

The ToolTipService attached properties need to be set on the element that owns the tooltip, not the tooltip itself.

You'll need to create a style for Button, or FrameworkElement.

Related