We help IT Professionals succeed at work.

TabItem Style Trigger Problem


I'm developing a WPF application which makes extensive use of the TabControl.  Part of the requirement is to style individual TabItem background colours, for example TabItem1 = Red, TabItem2 = Green etc etc, I'm using Styles and Templates to achieve this which is working nicely.  The problem I have is that when a tab IsSelected I must also change the background colour for the TabControl, ideally I would like to use a Trigger in the TabItem template to set the background colour for the TabControl using all xaml if possible? not sure if it your able to access a TabControl property within a TabItem template?

Being my first WPF application I would certainly appreciate any ideas, comments etc.


Watch Question

I'm assuming that what you're looking for is for the TabControl to always have the same Background as its selected TabItem. The easiest way to do that is going to be to simply bind the two together, as in the attached code. If this isn't what you were looking for, please describe it in more detail. Hope this helps!
<Window x:Class="WpfApplication1.Window1"
    Title="Window1" Height="300" Width="300">
        <Style TargetType="TabControl" x:Key="SelectedBackgroundStyle">
            <Setter Property="Background"
                    Value="{Binding SelectedItem.Background, RelativeSource={x:Static RelativeSource.Self}}">
        <TabControl x:Name="Tabs" Style="{StaticResource SelectedBackgroundStyle}">
            <TabItem Header="First" Background="Blue">
            <TabItem Header="Second" Background="Green"/>
            <TabItem Header="Third"  Background="Orange"/>

Open in new window


Perfect! thanks.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.