开发者

WPF : Is it possible to use a data template and a control template?

开发者 https://www.devze.com 2023-01-24 07:24 出处:网络
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 DataTem

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 :

  1. HeaderTemplate, which is a data template for the header of each item
  2. ContentTemplate, which is a data template for the content of each item
  3. 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号