How to add a tooltip for a datagrid header where the title text is generated dynamically?

I need to add a tooltip for the column heading DataGrid(Silverlight 4). I will generate the number of columns and the column heading text dynamically.

GridColumnCreation(....)
{
    IEnumerable allHeaderText = /* Linq query */; 
}

How to use this collection to set tooltip?

+5
source share
4 answers

This can be done using DataGridTextColumn and DataGridTextColumn.HeaderStyle. In the headerstyle tag, use ToolTipService and bind the content to the generated dynamic values. Here is a sample code for this ...

      

        <data:DataGrid.Columns>
            <data:DataGridTextColumn Header="First Name" Binding="{Binding FName}" >
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip First" />
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>

            <data:DataGridTextColumn Header="Last Name" Binding="{Binding LName}">
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip Second"></ToolTip>
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>

            <data:DataGridTextColumn Header="City" Binding="{Binding City}">
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip Third"></ToolTip>
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>
        </data:DataGrid.Columns>

    </data:DataGrid>

</Grid>

where Custdetails .. is something like this.

class Customer
{
    public string LName { set; get; }
    public string FName { set; get; }
    public string City { set; get; }
}

DataBinding ...

List<Customer> customers = new List<Customer>
                                    {
                                        new Customer { LName="Alan", FName="Ameen", City="New York" },
                                        new Customer { LName="Forgeard", FName="Steven", City="Mumbai" },
                                        new Customer { LName="Angur", FName="Paul", City="São Paulo" }
                                    };
dgCustDetails.ItemsSource = customers;

... .. ToolTip Binding ...

+3

, @Farukh:

<data:DataGridTextColumn.HeaderStyle>
  <Style TargetType="DataGridColumnHeader">
    <Setter Property="ToolTipService.ToolTipProperty"
            Value="Your tool tip here" />
  </Style>
</data:DataGridTextColumn.HeaderStyle>

, :

var style = new Style(typeof(DataGridColumnHeader));
style.Setters.Add(new Setter(ToolTipService.ToolTipProperty,
                             "Your tool tip here"));
column.HeaderStyle = style;
+21

In case this can help anyone. It works when using the TooTip property.

<DataGridTextColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="ToolTip" Value="{Binding}"/>
    </Style>
</DataGridTextColumn.HeaderStyle>
+9
source

If you don't want to create a new style for the heading, just add a TextBlock for the column heading and set the tooltip.

<DataGridTextColumn>
    <DataGridTextColumn.Header>
        <TextBlock Text="ColumnA" ToolTip="ColumnA Tooltip"/>
    </DataGridTextColumn.Header>
</DataGridTextColumn>
+3
source

All Articles