D3bug
D3bug

Reputation: 81

How to apply TabControl style to a specific tabcontrol?

I've a TabControl with TabControl.Resources defined at the beginning. This is working nicely but the problem is that I've another TabControl inside the parent TabControl, and I don't want the style to be applied to the child TabControl.

Here's an example:

<TabControl>
   <TabControl.Resources>
       some style and triggers
   </TabControl.Resources>
   <TabItem> 
      //Style correctly applied here - there is an external control with a tab item
   </TabItem>
</TabControl>

The external control is created by me, I simply split the xaml in other file, this has another TabControl and I don't the style of the parent tab to be applied there.

What's the solution?

Upvotes: 2

Views: 810

Answers (2)

Felix D.
Felix D.

Reputation: 5083

Your problem:

The problem is that your style has no x:Key

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:local="clr-namespace:WpfApplication1"        
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TabControl>
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Foreground" Value="Red"></Setter>
            </Style>
        </TabControl.Resources>
        <TabItem Header="Tab 01">
            //Style correctly applied 
    here there is an external control with a tab item
        </TabItem>
        <TabItem Header="Tab 02">//Style correctly applied 
    here there is an external control with a tab item
        </TabItem>
    </TabControl>
</Grid>

                  enter image description here


The solution:

To avoid a Style from being applied to all Controls of the same type definded in TargetType you need to provide a ResouceKey.

To apply the Style with an x:Key you now need to specify it in the Style property of the control you want to use it on.

This is done with: Style="{StaticResource StyleName}"

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:local="clr-namespace:WpfApplication1"        
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TabControl>
        <TabControl.Resources>
            <Style x:Key="CustomStyle" TargetType="TabItem">
                <Setter Property="Foreground" Value="Red"></Setter>
            </Style>
        </TabControl.Resources>
        <TabItem Header="Tab 01">
            //No Style will be applied
        </TabItem>
        <TabItem Header="Tab 02" Style="{StaticResource CustomStyle}">//Style correctly applied 
    here there is an external control with a tab item
        </TabItem>
    </TabControl>
</Grid>

This will only apply the Style to TabItems where you have set the Style.

                  StyleApplied

Upvotes: 2

Kylo Ren
Kylo Ren

Reputation: 8803

Like below code:

<TabControl>
    <TabControl.Resources>
        <Style TargetType="TabItem" x:Key="tabItemStyle">                
            <Setter Property="Foreground" Value="Red" />
            <Setter Property="Header" Value="Styled Header" />
        </Style>
    </TabControl.Resources>
    <TabItem Style="{StaticResource tabItemStyle}">

    </TabItem>
    <TabItem Header="Simple Header">

    </TabItem>
</TabControl>

OUTPUT:

output

Upvotes: 2

Related Questions