问题描述:

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.

网友答案:

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.

相关阅读:
Top