目前我需要在数据单元格列类型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>
我也尝试过:
- <DataGridTextColumn.CellStyle>
- <Style TargetType="DataGridCell">
- <Setter Property="ToolTip" Value="{Binding CountNumber}"/>
- <Setter Property="ToolTip.ContentStringFormat" Value="{}{0:00}"/>
- </Style>
- </DataGridTextColumn.CellStyle>
但他们都不行.例如,数字3应显示为03.有什么想法吗?
解决方法
试试这个:
- <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是对这个技巧的描述.引用:
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中获得StringFormat,你需要使用TextBlock设置ContentControl:
- <TextBlock.ToolTip>
- <ToolTip>
- <TextBlock Text="{Binding Path=Age,StringFormat=0\{0\}}" />
- </ToolTip>
- </TextBlock.ToolTip>
最重要的是在ToolTip中设置强制ContentControl,不一定,如我的示例中所示(使用DataGridTemplateColumn).