0

I have found how to create ToolTip for DataGridColumnHeader in WPF (and make sure that the Header is wrapped).

<Style x:Key="StartingTabToolTipHeaderStyle" TargetType="DataGridColumnHeader">
   <Setter Property="ToolTip" Value="{x:Static r:Resource.StartingTabToolTip}"/>
   <Setter Property="ContentTemplate">
      <Setter.Value>
         <DataTemplate>
            <TextBlock TextWrapping="Wrap" Text="{Binding}"/>
         </DataTemplate>
      </Setter.Value>
   </Setter>
</Style>

I can use this style in this way:

<DataGridComboBoxColumn Header="{x:Static r:Resource.StartingTab}" SelectedItemBinding="{Binding StartingTab}" 
                        HeaderStyle="{StaticResource StartingTabToolTipHeaderStyle}">

This solution is not nice because I need to create a separate style for each header because the tooltip is hardwired in the style. I would like a general style which can be used in this way:

<DataGridComboBoxColumn Header="{x:Static r:Resource.StartingTab}" SelectedItemBinding="{Binding StartingTab}" 
                        MyToolTip="{x:Static r:Resource.StartingTabToolTip}">

Any idea how to do it? May be with attached property?

2 Answers 2

1

You can set the ToolTipService.ToolTip attached property on each column and bind your values to it, because it is not used on columns.

<DataGridComboBoxColumn Header="{x:Static r:Resource.StartingTab}"
                        SelectedItemBinding="{Binding StartingTab}" 
                        HeaderStyle="{StaticResource StartingTabToolTipHeaderStyle}"
                        ToolTipService.ToolTip="{x:Static r:Resource.StartingTabToolTip}">

Then you can bind this text on the column in your header style using a RelativeSource binding.

<Style x:Key="StartingTabToolTipHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
   <Setter Property="ToolTip" Value="{Binding Column.(ToolTipService.ToolTip), RelativeSource={RelativeSource Self}}"/>
   <Setter Property="ContentTemplate">
      <Setter.Value>
         <DataTemplate>
            <TextBlock TextWrapping="Wrap" Text="{Binding}"/>
         </DataTemplate>
      </Setter.Value>
   </Setter>
</Style>

Now you only have a single style. If you want to avoid setting the style on each column, make it an implicit style by omitting the x:Key. Then it will be applied to all matching types in scope.

<Style TargetType="{x:Type DataGridColumnHeader}">
Sign up to request clarification or add additional context in comments.

5 Comments

this is exactly what OP is trying to avoid. He wants to set the tooltip style, without creating a header style for each column
@Bandook No, here he creates a single style, that does not hard-wire the tool tip text.
Thank you @thatguy Your answer was not perfect (a trigger was missing) but it lead me to here stackoverflow.com/a/2052199/5852947
@IstvanHeckl Sorry, am I missing something? Which trigger was missing? In the linked answer there is an IsMouseOver trigger, but that is not necessary, because a tool tip is displayed only on mouse over anyway. I have tested it and the column header tooltip is displayed as expected, the trigger does not change anything for me.
Hi @thatguy ! For me IsMouseOver was needed without it the tooltip just did not appear.
0

You could use DataGridTemplateColumn

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="yourHeader" HeaderStyle={StaticResource HeaderStyle1}">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <!--you can add any control such as ComboBox, Button etc-->
                    <TextBlock Text="{Binding Message}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <!--Editing cell template is not mandatory, only if you want allow user to edit a particular cell, shown here only for reference-->
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <!--When the user double clicks on this cell, it changes the control to TextBox (from TextBlock - see above) to allow for user input-->
                    <TextBox Text="{Binding Message}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <!--Second Column-->
        <DataGridTemplateColumn Header="secondHeader" HeaderStyle={StaticResource HeaderStyle2}">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemSource="{Binding SomeSource}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

This might seem a bit much for each column. But it gives you far more control & better scalability for future complex changes. Its somewhat trivial work put in during the initial dev phase.

Atleast thats my opinion since after the success of the concept, several further inputs/changes & new requirements come in to improve user interactivity with specific rules & logic on how/when the user can interact with UI Elements especially when altering data.

Edit - Your style can be exactly like you made i.e. targeting DataGridColumnHeader.

2 Comments

Dear @Bandook. I have used DataGridTemplateColumn in the past. My question is focused on the HeaderStyle. I do not want to create a new style for each column. Somehow I want to avoid the hard-wired tooltip in the HeaderStyle.
You want the tooltip applied only to the header (i.e. not the data rows) - this can be controlled only via the HeaderStyle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.