Working with a DataGrid on a recent project, I had to get ToolTips on all of the DataGrid column headers. At first go around, I had used the first thing that came to my head which was to insert a TextBlock into my Header and give it a ToolTip.
- <DataGridTemplateColumn.Header>
- <TextBlock Text="Column B" ToolTip="A short explanation of Column B"/>
- </DataGridTemplateColumn.Header>
This worked OK for a while, I got my expected result which was a ToolTip when a user moused over the header…although it wasn’t exactly what I wanted because they had to mouse over the text of my header, but I was OK with that.
Later on, another requirement came in, to allow the users to choose which columns were visible and which were hidden and that is when the flaw in my first approach was exposed (more on this later) and I found an easier way using a trigger to display tool tips and didn’t require creating a TextBlock in DataGridTemplateColumn.Header.
- <Style TargetType="{x:Type DataGridColumnHeader}">
- <Style.Triggers>
- <Trigger Property="IsMouseOver" Value="True">
- <Setter Property="ToolTip" Value="{Binding Column.(ToolTipService.ToolTip), RelativeSource={RelativeSource Self}}"/>
- </Trigger>
- </Style.Triggers>
- </Style>
- …
- <!– Use ToolTipService.ToolTip to assign the tooltip value –>
- <DataGridTemplateColumn x:Name="colA" Width="40*" IsReadOnly="True" Header="Column A" ToolTipService.ToolTip="Explanation of Column A">