How to format string for tooltip in DataGridTextColumn in WPF

.net, c#, wpf

Solution

Try this:

<DataGridTemplateColumn Width="260" Header="MySample">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Age}">
                <TextBlock.ToolTip>
                    <ToolTip>
                        <TextBlock Text="{Binding Path=Age, StringFormat=0\{0\}}" />
                    </ToolTip>
                </TextBlock.ToolTip>
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Here is a description of this trick. Quote:

A ToolTip is a content control, which means it doesn't really have a display model. Since the TextBox is designed to display text, the StringFormat binding property works as advertised. Button is another example of this. (Both derive from ContentControl).

The idea is to `StringFormat` earned in `ToolTip`, you need to set the `ContentControl` with `TextBlock`:

<TextBlock.ToolTip>
    <ToolTip>
        <TextBlock Text="{Binding Path=Age, StringFormat=0\{0\}}" />
    </ToolTip>
</TextBlock.ToolTip>

The main thing to you is to set the force `ContentControl` in the `ToolTip`, not necessarily, as in my example (with `DataGridTemplateColumn`).

Problem

Currently I need format the tooltip string in data cell column type `DataGridTextColumn` Here is my try: ``` <DataGrid.Columns> <DataGridTextColumn Header ="Count Number"> <DataGridTextColumn.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="ToolTip" Value="{Binding CountNumber, StringFormat={}{0:00}}"> </Setter> </Style> </DataGridTextColumn.CellStyle> <DataGridTextColumn.Binding> <Binding Path="CountNumber" StringFormat="{}{0:00}" UpdateSourceTrigger="PropertyChanged" /> </DataGridTextColumn.Binding> </DataGridTextColumn> <!-- other columns--> </DataGrid.Columns> ``` I also tried: ``` <DataGridTextColumn.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="ToolTip" Value="{Binding CountNumber}"/> <Setter Property="ToolTip.ContentStringFormat" Value="{}{0:00}"/> </Style> </DataGridTextColumn.CellStyle> ``` But both them don't work. For example, the number `3` should be display as `03`. Is there any idea?

Original source

Related problems