2
votes

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}" />

Working Tooltip

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>

Broken Tooltip

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:

Attempt

But of course, I don't want the textblock text property, I want the raw value the textblock is bound to.

1
Why not simply apply the style that works to the text box? aka <TextBlock Style="{StaticResource GridCell}"/>Tejs
@Tejs - Same effect. Changing the statcell to look like <xcdg:StatCell FieldName="Limit"><TextBlock Style="{StaticResource GridCell}"> results in the tooltip that says "System.Windows.DataTemplate"Alain
I just noticed this, is your key name supposed to be a reference? I thought x:Key must be a simple string (ex: x:Key="{x:Type TextBlock}" That looks odd to me. It may be nothing though (and it just that string value as the key).Tejs
Setting the x:Key to a type makes that style the default for all elements of that type. It's the same as if you were to specify a target type and no x:key at all - I just prefer to be explicit.Alain

1 Answers

0
votes

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.