WPF : Is it possible to use a data template and a control template?
I'm currently working on styling a TabControl, and have a maybe weird idea :
What if I would like to have a default ControlTemplate for all my tabs in my application, but specify a different DataTemplate for each TabControl (depending on the data I want to show)?
What do you think of that? Do you know if there's a solution for this?
M开发者_如何学编程aybe I'm taking the wrong way...
Thank you for your thoughts :-)
You cannot specify a DataTemplate for a TabControl you specify a DataTemplate for the data that is displayed in all TabItems of your TabControl.
I found a way! In fact, the ItemContainerStyle property of the TabControl provide three templates :
- HeaderTemplate, which is a data template for the header of each item
- ContentTemplate, which is a data template for the content of each item
- Template, which can be a control template, and which is the one I'm using!
Here's how I do it (the style) :
<Style x:Key="MyTabItemStyle" TargetType="{x:Type TabItem}">
<Setter Property="Template"
Value="{StaticResource ControlTemplate}"/>
<Setter Property="Header" Value="{Binding}"/>
<Setter Property="HeaderTemplate"
Value="{StaticResource HeaderTemplate}"/>
<Setter Property="Content" Value="{Binding}"/>
<Setter Property="ContentTemplate"
Value="{StaticResource ContentTemplate}"/>
</Style>
And to link it to the TabControl :
<TabControl ItemsSource="{Binding Elements}"
SelectedIndex="{Binding SelectedIndex}"
ItemContainerStyle="{DynamicResource MyTabItemStyle}">
What do you think of that?
精彩评论