简体   繁体   English

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

[英]How to format string for tooltip in DataGridTextColumn in WPF

Currently I need format the tooltip string in data cell column type DataGridTextColumn 目前我需要在数据单元格列类型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 . 例如,数字3应显示为03 Is there any idea? 有什么想法吗?

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. ToolTip是一个内容控件,这意味着它实际上没有显示模型。 Since the TextBox is designed to display text, the StringFormat binding property works as advertised. 由于TextBox旨在显示文本,因此StringFormat绑定属性的工作方式与广告一样。 Button is another example of this. 按钮是另一个例子。 (Both derive from ContentControl). (两者都来自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 )。

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. 我有一个与DataGridHyperlinkColumn类似的问题,我不想更改为DataGridTemplateColumn所以我提出了我认为更好的解决方案。 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: 您所要做的就是在<Setter...>打破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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM