Paya
Paya

Reputation: 5222

Forcing TabPanel to use specified TabItem template

So I have created my own TabControl and TabItem templates (both with x:Name attribute), and I use them like this:

    <Style TargetType="TabControl">
        <Setter Property="Template" Value="{DynamicResource MyTabControl}" />
    </Style>

As expected, this makes all TabControls in the window use MyTabControl template, however it still uses the old TabItem template. How can I make it work so the code above somehow manages to style every TabItem inside that template to use MyTabItem template?

Of course I could just edit the MyTabControl template and set the template manually, but that would require me to use something else than TabPanel container (probably StackPanel). Is it somehow possible to tell the TabPanel to use specific template for every TabItem?

In MyTabControl I use this to display TabItems:

<TabPanel IsItemsHost="True" />

And I'm looking for something like this (I don't know if this is possible):

<TabPanel IsItemsHost="True" TabItemsTemplate="MyTabItem" />

Upvotes: 1

Views: 1215

Answers (1)

thornhill
thornhill

Reputation: 631

I believe this would work: Create a style for your TabItem, that sets it's Template to your TabItem Template

<Style x:Key="MyTabItemStyle" TargetType={x:Type TabItem}">
  <Setter Property="Template" Value="{StaticResource TabItemTemplateName}" /> 
</Style>

and alter your above Style to:

<Style TargetType="TabControl">
   <Setter Property="Template" Value="{DynamicResource MyTabControl}" />
   <Setter Property="ItemContainerStyle" Value="{StaticResource MyTabItemStyle}" />
</Style>

You could also just ignore the above and create a style without a key for your TabItem, and this will set all TabItems to this style.

<Style TargetType={x:Type TabItem}">
  <Setter Property="Template" Value="{StaticResource TabItemTemplateName}" /> 
</Style>

You could also have a look here: http://blogs.intuidev.com/post/2010/01/25/TabControlStyling_PartOne.aspx

Upvotes: 2

Related Questions