4

Is there a way to add tool tip to DataGridColumn header and still retain the sorting functionality. The below code doesnt work(It doesnt display the tooltip)

<toolkit:DataGridTextColumn Header="Test" Width="70" Binding="{Binding TestText}" ToolTipService.ToolTip="{Binding TestText}">

And when I use the code below

<toolkit:DataGridTemplateColumn Header="Test" Width="70">  
              <toolkit:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding TestText}" ToolTip="{Binding TestText}"  />
                        </DataTemplate>
                    </toolkit:DataGridTemplateColumn.CellTemplate>
                </toolkit:DataGridTemplateColumn>

The column loses sorting functionality..Help!

developer
  • 5,178
  • 11
  • 47
  • 72
  • Do you want the tooltip to appear when the mouse hovers over the column header or when it hovers over a data item in the body of the grid? – Mike Schenk Jan 28 '11 at 23:42
  • Possible duplicate of [How do I Add a Tooltip To a DataGridTextColumn](http://stackoverflow.com/questions/1164288/how-do-i-add-a-tooltip-to-a-datagridtextcolumn) – Jay Wick Dec 16 '15 at 01:37

4 Answers4

8

To get the ToolTip to display in the DataGridColumnHeader you'll need to bind the ToolTip property for it to the ToolTip of its DataGridColumn like this

<toolkit:DataGridTextColumn Header="Test"
                            Width="70"
                            Binding="{Binding TestText}"
                            ToolTipService.ToolTip="My Tooltip Text">
    <toolkit:DataGridTextColumn.HeaderStyle>
        <Style TargetType="toolkit:DataGridColumnHeader">
            <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={RelativeSource Self},
                                    Path=Column.(ToolTipService.ToolTip)}"/>
        </Style>
    </toolkit:DataGridTextColumn.HeaderStyle>
</toolkit:DataGridTextColumn>
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
4

When the grid creates automatic columns, it knows which field is being displayed in that column. When you create the column yourself, the data grid doesn't know what data you'll be displaying in that column and so it cannot guess which field to sort the column by. To make a column you define yourself sortable, add the SortMemberPath property to your DataGridTemplateColumn like this:

<DataGridTemplateColumn Header="Test" Width="70" SortMemberPath="TestText">
    ...
</DataGridTemplateColumn>
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
1

Previous answers are mostly correct, however I find them overly complicated or addressing only one of the two concerns of the post.

Firstly, you can always set the SortPath property to maintain sorting for a DataGridTemplateColumn, or possibly when you want to sort on some property other than what is displayed.

Second, you do not need a DataGridTemplateColumn in order to have a ToolTip on the Column Header like the OP mentions. You might use a template column if you want to add a tooltip to the actual cell (but this probably isn't needed either). In any case, adding a ToolTip to the column header is most easily accomplished by the HeaderStyle

<DataGridTextColumn Header="Test" Binding="{Binding TestText}">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="ToolTip" Value="Test ToolTip" />
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
TCC
  • 2,546
  • 1
  • 24
  • 35
0

You are adding a tooltip to the column template, not to the header.

Have you tried setting the HeaderStyle property on DataGridColumn to a style that contains a template including a tooltip for the HeaderCell?

Have a look at this example too

Emond
  • 50,210
  • 11
  • 84
  • 115
  • I updated my question. I want the tooltip for all the records that appear below the Header in the column. Somehow the above code doesnt work.. – developer Jan 28 '11 at 22:42
  • @developer: Did you look at the example in my answer? I still don't understand what the effect is that you wish for. From your question: "add tool tip to DataGridColumn header" – Emond Jan 29 '11 at 07:24