c# - 工具提示的样式化文本 block 绑定(bind)改为绑定(bind)到控件模板

标签 c# wpf xaml binding

我正在尝试将文本 block 的工具提示绑定(bind)到文本 block 文本绑定(bind)到的值。

以下适用于应用此样式的文本 block :

<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

但由于某些奇怪的原因,当我尝试将此样式作为资源传递给数据网格统计单元格时,

<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

如您所见,工具提示被绑定(bind)到某个 DataTemplate,而不是文本框文本绑定(bind)到的任何对象。据我所知,这两者没有区别,事实上后者似乎更直接。

谁能弄清楚为什么第二个工具提示绑定(bind)不像第一个那样工作?


注意 我可以确定绑定(bind)正在进入单元格中的文本框,因为如果我将绑定(bind)更改为:

<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

当然,我不想要文本 block 文本属性,我想要文本 block 绑定(bind)到的原始值。

最佳答案

这是因为文本绑定(bind)正在查看工具提示附加到的对象的数据上下文。碰巧 xcdg:StatCell 出于自己的目的劫持了数据上下文,因此任何子视觉元素都无法访问绑定(bind)到的原始属性。

关于c# - 工具提示的样式化文本 block 绑定(bind)改为绑定(bind)到控件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7364089/

相关文章:

c# - 为选定的 ListView 项目禁用蓝色边框

c# - 有没有办法用 UTC 检查它是否是 DST(夏令时)?

c# - 如何为特定时区创建 TimeZone 对象? C#

c# - 在 asp.net 中嵌入字体

wpf - 嵌套 View 模型的 MVVM 命令绑定(bind)

wpf - 列表框数据模板-只能通过单击子元素来选择项目,而不仅仅是单击项目上的任何位置

wpf - 使用 Caliburn.Micro 组合框驱动的 UI 元素

c# - 医学图像处理的最佳编程语言

xaml - Xamarin 表单 : Swipe to delete(gesture) in ListView

image - 如何将图像背景设置为重复?