wpf - DataGridTextColumn 工具提示问题
问题描述
我有这种风格来向数据网格的每一行中的单元格添加工具提示。问题是所有工具提示都具有相同的文本(第一行的文本)。我究竟做错了什么?
<DataGridTextColumn Header="Profit (%)"
Binding="{Binding Percentage, StringFormat=N8}">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}"
BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<ToolTip.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content.Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}}" />
</DataTemplate>
</ToolTip.ContentTemplate>
</ToolTip>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
但是如果我使用一个简单的工具提示,它就完美了
<DataGridTextColumn Header="Profit (%)"
Binding="{Binding Percentage, StringFormat=N8}">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}"
BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="ToolTip"
Value="{Binding Percentage}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
解决方案
摆脱ContentTemplate
并将其设置为Tooltip
绑定TextBlock
到父级的 a DataGridCell
:
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="{Binding PlacementTarget.Content.Text,
RelativeSource={RelativeSource AncestorType=ToolTip}}" />
</ToolTip>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
推荐阅读
- python - py2exe 在导入熊猫时始终失败
- javascript - 更改附加元素的属性
- c++ - 地图(long long int,long long int)不适用于 key=1000 000 000(在 long long int 范围内)
- c# - 如何验证测试中没有与依赖项的交互
- typescript - 字典函数的正确返回类型
- handlebars.js - 车把代替 Sightly HTL
- php - 使用 Laravel 多次调整图像大小
- vespa - 关于使用空间词组语义规则的问题
- javascript - 如何缩短这个三元运算符?
- symfony4 - Symfony4:使用 URL 动态连接到数据库