工具提示的样式文本块绑定改为绑定到控件模板
我正在尝试将文本块的工具提示绑定到文本块文本所绑定的值。
以下内容适用于应用此样式的文本块:
<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}" />
但由于某些奇怪的原因,当我尝试将此样式作为资源传递给 datagrid stat 单元格,
<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>
如您所见,工具提示正在显示绑定到某些DataTemplate 而不是文本框文本绑定的任何内容。据我所知,这两者没有区别,事实上后者似乎更直接。
谁能弄清楚为什么第二个工具提示绑定不能像第一个那样工作?
注意我可以确定绑定正在通过单元格中的文本框,因为如果我将绑定更改为:
<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>
我会得到:
但是,当然,我不需要 textblock 文本属性,我想要 textblock 绑定到的原始值。
I'm trying to bind the tooltip of textblocks to the value the textblock text is bound to.
The following works for textblocks that apply this style:
<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}" />
But for some strange reason, when I try to pass this style in as a resource to datagrid stat cells,
<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>
As you can see, the tooltip is being bound to some DataTemplate rather than whatever the textbox text is bound to. From what I can tell though, there's no difference in these two, in fact the latter seems more straightforward.
Can anyone figure out why the second tooltip binding isn't working the way the first one is?
Note I can be sure that the binding is making its way though to the textbox in the cell, because if I change the binding to:
<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>
I get this:
But of course, I don't want the textblock text property, I want the raw value the textblock is bound to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是文本绑定正在查看工具提示所附加的对象的数据上下文。碰巧 xcdg:StatCell 出于自身目的劫持了数据上下文,因此任何子视觉元素都无法访问所绑定的原始属性。
The reason for this was that the text binding was looking at the datacontext of the object the tooltip was attached to. It just so happens that the xcdg:StatCell hijacks the datacontext for its own purposes, and so any child visual elements do not have access to the original property being bound to.