首页 文章

工具提示的样式化文本块绑定反而绑定到控件模板

提问于
浏览
1

我正在尝试将文本块的工具提示绑定到textblock文本绑定的值 .

The following works 适用于应用此样式的文本块:

<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

但是出于一些奇怪的原因,当我尝试将此样式作为资源传递给datagrid统计单元格时,

<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

如您所见,工具提示绑定到某些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>

我明白了:

Attempt

但是当然,我不想要textblock文本属性,我想要textblock绑定的原始值 .

1 回答

  • 0

    原因是文本绑定正在查看工具提示附加到的对象的datacontext . 恰好xcdg:StatCell为了自己的目的而劫持了datacontext,因此任何子视觉元素都无法访问被绑定的原始属性 .

相关问题