[英]WPF - reusable DataTemplate for ItemsControl/ListBox
I need to reuse the following DataTemplate: 我需要重用以下DataTemplate:
<DataTemplate x:Key="courseItemTemplate">
<Border BorderThickness="3" CornerRadius="5">
<Border.Background>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource CourseColorConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="DataContext.CourseColors"/>
<Binding Path="Course.CourseInfo.ID"/>
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
</Border.Background>
<ContentControl Content="{Binding}"/>
</Border>
</DataTemplate>
The template will serve both ListBox and an ItemsControl, with different ItemsSource, and different template for presenting the content of each item. 该模板将同时服务ListBox和ItemsControl,具有不同的ItemsSource和用于显示每个项目内容的不同模板。 Essentially, what I would like is to be able to replace the ContentControl tag, with the relevant template for each of the controls
本质上,我希望能够用每个控件的相关模板替换ContentControl标签
Use two DataTemplates with a common Border Style: 使用两个具有共同边框样式的DataTemplates:
<Style x:Key="BorderStyle" TargetType="Border">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush>...</SolidColorBrush>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="T1">
<Border Style="{StaticResource BorderStyle}">
<ContentPresenter Content="{Binding P1}"/>
</Border>
</DataTemplate>
<DataTemplate x:Key="T2">
<Border Style="{StaticResource BorderStyle}">
<ContentPresenter Content="{Binding P2}"/>
</Border>
</DataTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.