Search code examples
c#.netwpftabcontroltabitem

How to change a background of a single tab item header in WPF?


I've been stylizing a TabControl in WPF XAML (.NET 4), all i can do is style a tab based on triggers or identically style them all. Is there any way that only the first tab is stylized different while the other tabs are stylized different than the first tab but the same as each other (a.k.a using the Tab Index to stylize a TabItem).

Thank you.


Solution

  • You can use AlternationCount and AlternationIndex:

    <TabControl AlternationCount="{Binding Path=Items.Count,RelativeSource={RelativeSource Self}}">
        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex"
                                Value="0"> <!-- First item -->
                        <Setter Property="FontWeight"
                                Value="Bold"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TabControl.ItemContainerStyle>
        <TabItem Header="First"/>
        <TabItem Header="Second"/>
        <TabItem Header="Third"/>
        <TabItem Header="Fourth"/>
    </TabControl>