gpt4 book ai didi

wpf - WPF 中是否有类似于 DataTemplate 的 Style.BasedOn 的东西?

转载 作者:行者123 更新时间:2023-12-02 00:36:37 25 4
gpt4 key购买 nike

目前,我有两个非常大的 DataTemplate 对象来显示两个列表框中的两组项目。在两个 ListBox 的 ItemContainerStyle 属性中设置的两个 Styles 的 ContentTemplate 属性中引用了 DataTemplates。这些项目是相同类型的,并且 DataTemplates 是相同的,除了以下控件:

来自 DataTemplate1

<TextBlock Style="{StaticResource TextStyle}" FontSize="20" Foreground="White"
HorizontalAlignment="Left" Panel.ZIndex="2" Text="{Binding RemainingTime.TotalHours,
Converter={StaticResource DoubleToIntegerConverter}, StringFormat={}{0:#00}}" />

来自 DataTemplate2

<TextBlock Style="{StaticResource TextStyle}" FontSize="20" Foreground="White"
HorizontalAlignment="Left" Panel.ZIndex="2" Text="{Binding ElapsedTime.TotalHours,
Converter={StaticResource DoubleToIntegerConverter}, StringFormat={}{0:#00}}" />

有没有办法避免复制整个 Dataemplate 但在第二个模板中此 TextBlock 的文本绑定(bind)仍然存在这一差异?

最佳答案

不,DataTemplate 没有继承。如果您考虑一下,您将如何覆盖 DataTemplate 的一部分?

解决方案:使用另一个Style 来捕获两个模板之间的公共(public)属性。如果它只放在您需要的地方,您可以将它的范围限定在同一个 Resources block 中。这是更简洁或更 WPF 的做事方式。

关于wpf - WPF 中是否有类似于 DataTemplate 的 Style.BasedOn 的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4355045/

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