9

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>
denza
  • 1,298
  • 4
  • 24
  • 49

3 Answers3

11

You could do this:

<GridViewColumn Width="50"
                DisplayMemberBinding="{Binding Path=RedenBroj}">
    <GridViewColumn.Header>
        <TextBlock Text="Ред.Број"
                   ToolTip="Your explanation" />                      
    </GridViewColumn.Header>        
</GridViewColumn>
LPL
  • 16,827
  • 6
  • 51
  • 95
10

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>
Community
  • 1
  • 1
Scroog1
  • 3,539
  • 21
  • 26
  • 4
    I think it could be a little bit less verbose (``) but better solution than mine. +1 – LPL Apr 23 '15 at 13:36
  • How could I use another value from the GridViewColumn to display a different ToolTip than the content already visible in the header? I tried it with the "Tag" property but although I could use "Tag" as binding in the setter value, I can't assign a "Tag" property to the GridViewColumn like this: – Aaginor Apr 22 '20 at 11:34
0

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);
    }
}
metoyou
  • 639
  • 1
  • 7
  • 22