在WPF中有什么类似于DataTemplate的Style.BasedOn? [英] Is there something in WPF similar to Style.BasedOn for DataTemplate?
问题描述
从DataTemplate1
< TextBlock Style ={StaticResource TextStyle}FontSize =20Foreground =White
HorizontalAlignment =LeftPanel.ZIndex =2Text ={ Binding RemainingTime.TotalHours,
Converter = {StaticResource DoubleToIntegerConverter},StringFormat = {} {0:#00}}/>
从DataTemplate2
< TextBlock Style ={StaticResource TextStyle}FontSize =20Foreground =White
HorizontalAlignment =LeftPanel.ZIndex =2Text ={Binding ElapsedTime.TotalHours ,
Converter = {StaticResource DoubleToIntegerConverter},StringFormat = {} {0:#00}}/>
有没有办法避免重复整个Dataemplate,但在文本绑定中仍然有一个区别这个TextBlock在第二个模板?
我从另一篇文章(由Liz)得到一个答案。基本上,您可以将所有常用控件放在一个DataTemplate中,然后再创建两个DataTemplates,每个DataTemplate都使用第一个DataTemplate作为ContentPresenter中的ContentTemplate。然后,您可以在后一个DataTemplates中的一个或两个中添加不同的控件。 Liz提供了一个代码示例。
< DataTemplate x:Key =UserTemplate>
<! - 显示用户类的所有属性 - >
< / DataTemplate>
< DataTemplate DataType ={x:Type local:User}>
< ContentPresenter Content ={Binding}ContentTemplate ={StaticResource UserTemplate}/>
< / DataTemplate>
< DataTemplate DataType ={x:Type local:Author}>
< StackPanel>
< ContentPresenter Content ={Binding}ContentTemplate ={StaticResource UserTemplate}/>
<! - 显示所有其他作者属性 - >
< / StackPanel>
< / DataTemplate>
再次感谢Liz。
At the moment, I have two very large DataTemplate objects to display two sets of items in two ListBoxes. The DataTemplates are referenced in the ContentTemplate property in two Styles that are set in the ItemContainerStyle properties of the two ListBoxes. The items are of the same type and the DataTemplates are identical except for the following control:
From DataTemplate1
<TextBlock Style="{StaticResource TextStyle}" FontSize="20" Foreground="White"
HorizontalAlignment="Left" Panel.ZIndex="2" Text="{Binding RemainingTime.TotalHours,
Converter={StaticResource DoubleToIntegerConverter}, StringFormat={}{0:#00}}" />
From DataTemplate2
<TextBlock Style="{StaticResource TextStyle}" FontSize="20" Foreground="White"
HorizontalAlignment="Left" Panel.ZIndex="2" Text="{Binding ElapsedTime.TotalHours,
Converter={StaticResource DoubleToIntegerConverter}, StringFormat={}{0:#00}}" />
Is there some way to avoid duplicating the whole Dataemplate but still have this one difference in the text binding of this TextBlock in the second template?
I got an answer to this from another post (by Liz). Basically, you can put all common controls into one DataTemplate and then create two more DataTemplates that each use the first one as a ContentTemplate in a ContentPresenter. Then, you can add different controls into one or both of the latter DataTemplates. Liz provided a code example.
<DataTemplate x:Key="UserTemplate">
<!-- show all the properties of the user class here -->
</DataTemplate>
<DataTemplate DataType="{x:Type local:User}">
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource UserTemplate}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Author}">
<StackPanel>
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource UserTemplate}"/>
<!-- show all the additional Author properties here -->
</StackPanel>
</DataTemplate>
Thanks once again Liz.
这篇关于在WPF中有什么类似于DataTemplate的Style.BasedOn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!