gpt4 book ai didi

带有 TextBlock 的 WPF DataGridCell 模板 - 绑定(bind)?

转载 作者:行者123 更新时间:2023-12-04 21:06:45 24 4
gpt4 key购买 nike

我替换了ContentPresenterDataGridCellTemplateTextBlock现在我搜索正确的 Binding到内容。

正常方式是Text="{TemplateBinding Content}对于TextBlock - 它不起作用。还有Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content, Mode=TwoWay}"不正确。

还有其他想法吗?

最佳答案

假设您更改了 DataGridCell Template到以下

<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<TextBlock Text="{Binding}"/>
<!--<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> -->
</Border>
</ControlTemplate>

由于您删除了 ContentPresenter , DataGridCell无法显示其 Content .它仍然存在。 DataGridCell.ContentTextBlock包含您的原始 TextTextBlockTemplate是另一个。

所以你会得到正确的 Text通过将其绑定(bind)到 Content.Text TemplatedParent 的属性(property)
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Content.Text}"/>

所以,总结一下。这有效
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Content.Text}"/>
</Border>
</ControlTemplate>

关于带有 TextBlock 的 WPF DataGridCell 模板 - 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7159978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com