如何在WPF中的DataGridTextColumn中格式化工具提示的字符串

时间:2022-10-14 07:27:46

Currently I need format the tooltip string in data cell column type DataGridTextColumn
Here is my try:

目前我需要在数据单元格列类型DataGridTextColumn中格式化工具提示字符串这是我的尝试:

<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?

但他们都不行。例如,数字3应显示为03.有什么想法吗?

2 个解决方案

#1


5  

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

ToolTip是一个内容控件,这意味着它实际上没有显示模型。由于TextBox旨在显示文本,因此StringFormat绑定属性的工作方式与广告一样。 Button就是另一个例子。 (两者都来自ContentControl)。

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

想法是在ToolTip中获得StringFormat,你需要使用TextBlock设置ContentControl:

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

最重要的是在ToolTip中设置强制ContentControl,不一定,如我的示例中所示(使用DataGridTemplateColumn)。

#2


1  

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:

我有一个与DataGridHyperlinkColumn类似的问题,我不想更改为DataGridTemplateColumn,所以我提出了我认为更好的解决方案。您所要做的就是在 中分解Value的设置,并将内容放在TextBlock中,如下所示:

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

#1


5  

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

ToolTip是一个内容控件,这意味着它实际上没有显示模型。由于TextBox旨在显示文本,因此StringFormat绑定属性的工作方式与广告一样。 Button就是另一个例子。 (两者都来自ContentControl)。

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

想法是在ToolTip中获得StringFormat,你需要使用TextBlock设置ContentControl:

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

最重要的是在ToolTip中设置强制ContentControl,不一定,如我的示例中所示(使用DataGridTemplateColumn)。

#2


1  

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:

我有一个与DataGridHyperlinkColumn类似的问题,我不想更改为DataGridTemplateColumn,所以我提出了我认为更好的解决方案。您所要做的就是在 中分解Value的设置,并将内容放在TextBlock中,如下所示:

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