Search code examples
wpfdependency-propertieswpf-style

How to use depenceny property in the style of tooltip to set the text?


In a button, I am using a dependency property to pass information from the view model to the style of the button, so I can set the color of the color according to some conditions.

The code for the button is this:

The style in my xaml file:

<Style x:Key="BotonesColorEstadosTemplate" TargetType="{x:Type Button}">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type Button}">
        <ControlTemplate.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="dp:BotonesEstadosAttachedProperty.CodigoEstado" Value="0"/>
                    <Condition Property="dp:BotonesEstadosAttachedProperty.AccionHabilitada" Value="true"/>
                </MultiTrigger.Conditions>
            </MultiTrigger>
        <ContentTemplate>
    </ContentTemplate>
</Style>

The dependency properties:

public static class BotonesEstadosAttachedProperty
{
    public static readonly DependencyProperty CodigoEstadoProperty =
        DependencyProperty.RegisterAttached(
        "CodigoEstado",
        typeof(short),
        typeof(BotonesEstadosAttachedProperty));

    public static short GetCodigoEstado(DependencyObject obj)
    {
        return (short)obj.GetValue(CodigoEstadoProperty);
    }

    public static void SetCodigoEstado(DependencyObject obj, short value)
    {
        obj.SetValue(CodigoEstadoProperty, value);
    }


    public static readonly DependencyProperty AccionHabilitadaProperty =
        DependencyProperty.RegisterAttached(
        "AccionHabilitada",
        typeof(bool),
        typeof(BotonesEstadosAttachedProperty));

    public static bool GetAccionhabilitada(DependencyObject obj)
    {
        return (bool)obj.GetValue(AccionHabilitadaProperty);
    }

    public static void SetAccionHabilitada(DependencyObject obj, bool value)
    {
        obj.SetValue(AccionHabilitadaProperty, value);
    }
}

How to use in the button:

<Button Name="btnAlmacenesActualizar" Content="..." Height="23" Margin="3,0,0,0" Width="23"
    ap:BotonesEstadosDependencyProperty.CodigoEstado="{Binding CodigoEstadoActualizarAlmacenes}"
    ap:BotonesEstadosDependencyProperty.AccionHabilitada="{Binding EsAccionActualizarAlmacenesHabilitada}">

With this, I can use a property of my view model and pass to my style, that use the information in the trigger to set the color of the button.

Now, I would like to have a style for the tooltip, to have the default configuration for all the tooltips, and I would like to can pass the text of the tooltip at first, but later I would like to pass another variables. By the moment, to test, I would like to try with the text.

I have this style:

<Style TargetType="ToolTip" x:Key="ToolTipDefaultStyle">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel>
                    <TextBlock>
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Text" Value="dp:ToolTipAttachedProperty.Texto"/>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

My StackPanel with the tooltip:

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

But in this case the text that is shown is "dp:ToolTipAttachedProperty.Texto". So I would like if it is possible to do the same than with the button, use a dependency propety to pass information from view model to the style.

Thanks.


Solution

  • You are currently not binding the attached property, you are assigning a string as Value. For binding to attached properties, you need to use the binding markup extension and parentheses, e.g.:

    <Setter Property="Text" Value="{Binding (dp:ToolTipAttachedProperty.Texto)"/>
    

    However, in your case you need to refer to the parent control of ToolTip, which your property is attached to. Normally, you would do this with a RelativeSource binding and AncestorType, but this does not work here, because ToolTip is not within the same visual tree as the parent control.

    Instead, you can access the control via the PlacementTarget property on the parent ToolTip.

    <Setter Property="Text" Value="{Binding PlacementTarget.(dp:ToolTipAttachedProperty.Texto), RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/>
    

    Please also check your XAML for typos. The attached properties type does not match on the StackPanel and in the ToolTip style: ToolTipDependencyProperty or ToolTipAttachedProperty?