Thursday, June 11, 2015

Tooltips on DataGrid headers

I have a DataGridTextColumn which has a dynamically generated tooltip on each cell (binding the cell's tooltip via a multi-value converter) but I want a static tooltip on the column header that explains what the user is looking at. It turns out to be quite easy once you know how to do it.

Here is the original DataGridTextColumn definition...

<DataGridTextColumn Header="Total Pending" Width="60" Binding="{Binding Path=TotalQuantityPending, StringFormat=0.00;;#}" ElementStyle="{StaticResource TextBlockRightAlign}" IsReadOnly="true">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridCell}">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ConversionConverter}">
                        <Binding Path="UORCode"/>
                        <Binding Path="UOICode"/>
                        <Binding Path="Conversion"/>
                        <Binding Path="TotalQuantityReturned"/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

Now to add a static tooltip to the header we take advantage of the fact that the header can be any content control. So we make it a textblock with it's own tooltip. Simple when you think of it.

<DataGridTextColumn Width="60" Binding="{Binding Path=TotalQuantityPending, StringFormat=0.00;;#}" ElementStyle="{StaticResource TextBlockRightAlign}" IsReadOnly="true">
    <DataGridTextColumn.Header>
        <TextBlock Text="Total Pending" ToolTip="Total received on this line on all Pending receipts."></TextBlock>
    </DataGridTextColumn.Header>
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridCell}">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ConversionConverter}">
                        <Binding Path="UORCode"/>
                        <Binding Path="UOICode"/>
                        <Binding Path="Conversion"/>
                        <Binding Path="TotalQuantityReturned"/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

No comments:

Post a Comment