Question

I have a ContentPresenter inside a ControlTemplate trying to layout a TabItem. I want the foreground color of the TextBlock inside the header to change color when the tab is selected. My template is the following:

<TabControl.Resources>
    <Style TargetType="TabPanel">
        <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
    <Style TargetType="TabControl">
        <Setter Property="BorderThickness" Value="0,0,0,0"></Setter>
    </Style>
    <Style TargetType="TabItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <ContentPresenter x:Name="ContentSite"
                                         VerticalAlignment="Center"
                                         HorizontalAlignment="Center"
                                         ContentSource="Header"
                                         Margin="30,10,30,10"/>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="ContentSite" Property="TextBlock.Foreground" Value="DarkGoldenrod"></Setter>
                        </Trigger>    
                        <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="ContentSite" Property="TextBlock.Foreground" Value="Black"></Setter>
                        </Trigger>    
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TabControl.Resources>

Unfortunately the triggers are doing nothing. Several other posts here on SO show a Setter tag that looks exactly like mine, using TargetName to make sure we only change the header and not the whole tab. The property is recognized and compiles, but nothing happens.

What the heck is going on, and what am I doing wrong? This has been driving me nuts for hours!

Was it helpful?

Solution

Got it!

I had the header like this:

<TabItem.Header>
    <TextBlock>My Header</TextBlock>
</TabItem.Header>

Problem was, there's already a TextBlock inside the header, you don't need to wrap the text again.

<TabItem.Header>
    My Header
</TabItem.Header>

And it works.

OTHER TIPS

Works exactly as expected for me, your problem may be lying somewhere else...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top