在WPF中有什么类似于DataTemplate的Style.BasedOn? [英] Is there something in WPF similar to Style.BasedOn for DataTemplate?

查看:297
本文介绍了在WPF中有什么类似于DataTemplate的Style.BasedOn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



从DataTemplate1

 < TextBlock Style ={StaticResource TextStyle}FontSize =20Foreground =White
Horizo​​ntalAlignment =LeftPanel.ZIndex =2Text ={ Binding RemainingTime.TotalHours,
Converter = {StaticResource DoubleToIntegerConverter},StringFormat = {} {0:#00}}/>

从DataTemplate2

 < TextBlock Style ={StaticResource TextStyle}FontSize =20Foreground =White
Horizo​​ntalAlignment =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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆