我正在尝试将文本 block 的工具提示绑定(bind)到文本 block 文本绑定(bind)到的值。
以下适用于应用此样式的文本 block :
<Style x:Key="GridCell" TargetType="{x:Type TextBlock}">
<Setter Property="ToolTip" Value="{Binding Converter={StaticResource CellToolTipConverter}}"/>
</Style>
<DataTemplate x:Key="GridCellContentTemplate">
<TextBlock Style="{StaticResource GridCell}"
Text="{Binding Converter=..."/>
</DataTemplate>
<xcdg:Column FieldName="FXRate" CellContentTemplate="{GridCellContentTemplate}" />
但由于某些奇怪的原因,当我尝试将此样式作为资源传递给数据网格统计单元格时,
<Style x:Key="{x:Type xcdg:StatCell}" TargetType="{x:Type xcdg:StatCell}">
<Style.Resources>
<Style x:Key="{x:Type TextBlock}" TargetType="{x:Type TextBlock}">
<Setter Property="ToolTip" Value="{Binding Converter={StaticResource CellToolTipConverter}}"/>
</Style>
</Style.Resources>
</Style>
<xcdg:StatCell FieldName="Limit">
<TextBlock Text="{Binding Source={StaticResource Layers}, Path=StatLimit, Converter=..." />
</xcdg:StatCell>
如您所见,工具提示被绑定(bind)到某个 DataTemplate,而不是文本框文本绑定(bind)到的任何对象。据我所知,这两者没有区别,事实上后者似乎更直接。
谁能弄清楚为什么第二个工具提示绑定(bind)不像第一个那样工作?
注意 我可以确定绑定(bind)正在进入单元格中的文本框,因为如果我将绑定(bind)更改为:
<Style x:Key="{x:Type xcdg:StatCell}" TargetType="{x:Type xcdg:StatCell}">
<Style.Resources>
<Style x:Key="{x:Type TextBlock}" TargetType="{x:Type TextBlock}">
<Setter Property="ToolTip" Value="{Binding Path=Text, RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource CellToolTipConverter}}"/>
</Style>
</Style.Resources>
</Style>
我明白了:
当然,我不想要文本 block 文本属性,我想要文本 block 绑定(bind)到的原始值。
最佳答案
这是因为文本绑定(bind)正在查看工具提示附加到的对象的数据上下文。碰巧 xcdg:StatCell 出于自己的目的劫持了数据上下文,因此任何子视觉元素都无法访问绑定(bind)到的原始属性。
关于c# - 工具提示的样式化文本 block 绑定(bind)改为绑定(bind)到控件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7364089/