Home > OS >  I get System.Windows.Style as text of the ToolTip of the header in a DataGrid
I get System.Windows.Style as text of the ToolTip of the header in a DataGrid

Time:03-03

I want to define a style for the datagrid columns that get the text of the tooltip through an attached property. But I get the text System.Windows.Style instead of the text.

The code is this. XML resource file that defines the style:

<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridColumnHeaderConTooltip">
    <Setter Property="ToolTip">
        <Setter.Value>
            <Style TargetType="ToolTip">
                <Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel>
                                <!--Para poder utilizar el attached propery, se tiene que utilizar PlacementTarget, y además indicar que el source
                                es el control padre, que es el tooltip, porque el TextBlck no pertenece al mismo visual tree.-->
                                <TextBlock Text="{Binding PlacementTarget.(ap:CabeceraDatagridAttachedProperty.Tooltip), RelativeSource={RelativeSource AncestorType=ToolTip}}"  MaxWidth="400" TextWrapping='Wrap' />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

The code in the xaml:

            <DataGridTextColumn Header="Cantidad Para Descontar" Binding="{Binding CantidadParaDescontar, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnDataErrors=True}" Width="AUTO" IsReadOnly="false"
                                ap:CabeceraDatagridAttachedProperty.Tooltip="tooltip cabecera por attached property"
                                HeaderStyle="{StaticResource DataGridColumnHeaderConTooltip}">

The attached property:

namespace GTS.CMMS.Client.AttachedProperties
{
    public static class CabeceraDatagridAttachedProperty
    {
        public static readonly DependencyProperty TooltipProperty =
            DependencyProperty.RegisterAttached(
            "Tooltip",
            typeof(string),
            typeof(CabeceraDatagridAttachedProperty));

        public static string GetTooltip(DependencyObject obj)
        {
            return (string)obj.GetValue(TooltipProperty);
        }

        public static void SetTooltip(DependencyObject obj, string value)
        {
            obj.SetValue(TooltipProperty, value);
        }
    }
}

CodePudding user response:

I get the text System.Windows.Style instead of the text.

This is expected, since you assigned a style to the ToolTip property instead of content. The ToolTip does not have any idea how to display a Style, so it calls ToString().

What you should do is bind the desired attached property directly to the ToolTip property. Use Self as RelativeSource to refer to the underlying DataGridColumnHeader. Then navigate to its Column property and specify your attached property.

<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridColumnHeaderConTooltip">
   <Setter Property="ToolTipService.ShowOnDisabled" Value="True"/>
   <Setter Property="ToolTip" Value="{Binding Column.(local:CabeceraDatagridAttachedProperty.Tooltip), RelativeSource={RelativeSource Self}}"/>
</Style>
  • Related