Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format string for tooltip in DataGridTextColumn in WPF

Tags:

c#

.net

wpf

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?

like image 241
ductran Avatar asked Aug 06 '13 09:08

ductran


2 Answers

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).

like image 137
Anatoliy Nikolaev Avatar answered Oct 18 '22 23:10

Anatoliy Nikolaev


I had a similar problem with a DataGridHyperlinkColumn that I didn't want to change to a DataGridTemplateColumn so I came up with what I think is an even better solution. All you have to do is break out the setting of the Value in your <Setter...> and put the content in a TextBlock like this:

<DataGridTextColumn Header ="Count Number">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <TextBlock Text="{Binding CountNumber, StringFormat={}{0:00}}" />
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.CellStyle>
    <DataGridTextColumn.Binding>
        <Binding Path="CountNumber" StringFormat="{}{0:00}" UpdateSourceTrigger="PropertyChanged" />
    </DataGridTextColumn.Binding>
</DataGridTextColumn>
like image 28
Mageician Avatar answered Oct 18 '22 21:10

Mageician