如何在 WPF 中的 DataGridTextColumn 中格式化工具提示的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18076620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format string for tooltip in DataGridTextColumn in WPF
提问by R4j
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 3should be display as 03. Is there any idea?
但它们都不起作用。
例如,数字3应显示为03。有什么想法吗?
采纳答案by Anatoliy Nikolaev
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>
Hereis 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 绑定属性按宣传的方式工作。按钮是另一个例子。(两者都源自 ContentControl)。
The idea is to StringFormatearned in ToolTip, you need to set the ContentControlwith TextBlock:
我们的想法是StringFormat获得了ToolTip,你需要设置ContentControl有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 ContentControlin the ToolTip, not necessarily, as in my example (with DataGridTemplateColumn).
你主要的是设置力ContentControl的ToolTip,没有必要,因为在我的例子(带DataGridTemplateColumn)。
回答by BrianVPS
I had a similar problem with a DataGridHyperlinkColumnthat I didn't want to change to a DataGridTemplateColumnso I came up with what I think is an even better solution. All you have to do is break out the setting of the Valuein your <Setter...>and put the content in a TextBlocklike this:
我有一个类似的问题DataGridHyperlinkColumn,我不想更改为 a,DataGridTemplateColumn所以我想出了我认为更好的解决方案。所有你需要做的是打出来的设置Value在你<Setter...>,把内容在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>

