How to add tooltip text on my Gridview column header, WPF VS2010
-
16-06-2021 - |
Question
Here is my grid i what to give an explanation to the header "RED.BROJ" when on mouse over that header to show the expl. text.
<ListView.View>
<GridView>
<GridViewColumn Width="50"
Header="Реd.Број"
DisplayMemberBinding="{Binding Path=RedenBroj}">
</GridViewColumn>
Solution
You could do this:
<GridViewColumn Width="50"
DisplayMemberBinding="{Binding Path=RedenBroj}">
<GridViewColumn.Header>
<TextBlock Text="Ред.Број"
ToolTip="Your explanation" />
</GridViewColumn.Header>
</GridViewColumn>
OTHER TIPS
Slightly late response but you can add a tooltip, without losing the ability to drag columns to reorder them, by doing the following:
<GridViewColumn Width="50"
Header="Реd.Број"
DisplayMemberBinding="{Binding Path=RedenBroj}">
<GridViewColumn.HeaderContainerStyle>
<Style>
<Setter Property="Control.ToolTip" Value="Tool tip content"/>
</Style>
</GridViewColumn.HeaderContainerStyle>
</GridViewColumn>
Update: more concise version thanks to LPL
Further update: I wanted to be able to have all columns have tooltips that match their headers (as some columns were too narrow to show the whole header):
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="ToolTip"
Value="{Binding Content, RelativeSource={RelativeSource Self}}"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn DisplayMemberBinding="{Binding A}" Header="A"/>
<GridViewColumn DisplayMemberBinding="{Binding B}" Header="B"/>
<GridViewColumn DisplayMemberBinding="{Binding C}" Header="C"/>
</GridView>
</ListView>
Nothing like answering an old question with your own...
I was inspired by @Scroog1's answer but seems a bit redundant having a Tooltip
which just mimics the content that is there. You usually want the Tooltip
because you've abbreviated the column header text.
I created a small AttachedProperty
which I set my Tooltip
value on the GridViewColumn
. I then bind
to this from my Style
for my GridViewColumnHeader
.
Now I just define the Style
once, and add it and the AttachedProperty
where I want to use it.
Xaml
<Style x:Key="GridViewColumnHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="ToolTip" Value="{Binding Path=Column.(attachedProperties:GridViewColumnHeaderToolTipAttachedProperty.Tooltip), RelativeSource={RelativeSource Self}}" />
</Style>
<GridView x:Key="GridViewFuelConsumption"
x:Shared="False">
<GridViewColumn Header="Ред.Број"
DisplayMemberBinding="{Binding RedenBroj}"
HeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle}"
attachedProperties:GridViewColumnHeaderToolTipAttachedProperty.Tooltip="Your explanation" />
</GridView>
AttachedProperty
public sealed class GridViewColumnHeaderToolTipAttachedProperty : DependencyObject
{
public static readonly DependencyProperty TooltipSourceProperty = DependencyProperty.RegisterAttached(
"Tooltip",
typeof(string),
typeof(GridViewColumnHeaderToolTipAttachedProperty),
new PropertyMetadata("null"));
public static void SetTooltip(DependencyObject element, string value)
{
element.SetValue(TooltipSourceProperty, value);
}
public static string GetTooltip(DependencyObject element)
{
return (string)element.GetValue(TooltipSourceProperty);
}
}