Search code examples
.netwpfxaml.net-4.0tabitem

Change tab item header background colour on loss of focus?


I want the background colour of my tab item's header to change when it looses focus. I was wondering what the best way to achieve this is, is it possible to achieve a purely XAML solution?


Solution

  • I don't know the best way. I know this way.

    <Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTest"
        Title="MainWindow" Height="350" Width="300">
    <Window.Resources>
        <Style TargetType="TabItem">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="False">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TabControl Height="100" Name="tabControl1" Width="200">
            <TabItem Header="tabItem1" Name="tabItem1">
                <Grid />
            </TabItem>
            <TabItem Header="tabItem2" Name="tabItem2">
                <Grid />
            </TabItem>
        </TabControl>
    </StackPanel>
    </Window>