WPF TabControl, change the background color of the TabItem with C# codes

The reason you're finding it difficult to get answers to your question is because you're going about it completely the wrong way; I can think of very few cases where changing controls in code as you're suggesting would be justified. WPF has been specifically designed to decouple visual state from code, ignore this at your own peril!

To actually answer your question though the following will do the trick...sort of...

foreach (TabItem item in tabControl.Items)
    item.Background = new SolidColorBrush(Colors.Blue);

If you do this then you'll notice it changes the background color of unselected tabs but not the currently selected tab. You'll also notice that highlighting a tab with the mouse will display another color yet again. There are in fact 7 different visual states for TabItem, adding code to cover each of these cases starts to get messy and certainly a lot more complex than using XAML.

The "proper" way to control background color via code is with XAML and data binding. First of all go to the Microsoft MSDN page for WPF TabControl Styles and Templates where you'll find the complete template for TabItem. Paste that into your Window.Resources section and now you can start playing around with the way it looks (don't forget to the add the namespaces and resources as well). If you play around with the various resources you'll notice that TabItems use a linear gradient, ControlLightColor is used for the top of all tabs, ControlMediumColor is used for the bottom of unselected tabs and ControlDarkColor is used for the bottom of the currently selected tab. To control this at run time you need to find all the instances of these resources scattered within the style and bind them to a property in your code, then write a ValueConverter that converts whatever your property is (I'm guessing a boolean) to a brush. Another (better) method, and one that doesn't require any extra code to be written at all, would be to use a DataTrigger that changes the visual appearence of the TabItem in response to your property changing value, but that's getting a little bit past the "beginner" stage and you'd need to provide more info on your specific case.

UPDATE: This seems to work for me:

void TabSet_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        foreach (TabItem item in tabControl.Items)
            item.Background = new SolidColorBrush(item.IsSelected ? Colors.Green : Colors.Red);
    }

I still say this is horribly wrong though. If you absolutely insist on doing this in code then you shouldn't be using WPF. It's completely the wrong technology, I cannot stress this strongly enough!

UPDATE #2: You're almost there, you just need to do this in the window that hosts the tab control:

<Window x:Class="MyWpfApplication.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" WindowState="Maximized">

    <Window.Resources>

        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border  Name="Border" Margin="0,0,-4,0" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" >
                                <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>

                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Panel.ZIndex" Value="100" />
                                <Setter TargetName="Border" Property="Background" Value="Red" />
                                <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                            </Trigger>

                            <Trigger Property="IsSelected" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="Green" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="Orange" />
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>

    <TabControl>
        <TabItem Header="Foo" />
        <TabItem Header="Bar" />
        <TabItem Header="Baz" />
    </TabControl>

</Window>

Tags:

C#

Wpf