工具提示的样式文本块绑定改为绑定到控件模板

发布于 2024-12-03 22:44:22 字数 2096 浏览 0 评论 0原文

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

以下内容适用于应用此样式的文本块:

<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 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>

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 绑定到的原始值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-12-10 22:44:22

原因是文本绑定正在查看工具提示所附加的对象的数据上下文。碰巧 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文