Setting TabItem foreground color also sets the TabControl foreground color Setting TabItem foreground color also sets the TabControl foreground color wpf wpf

Setting TabItem foreground color also sets the TabControl foreground color


I have just come across this very same problem, and after fiddling around with it a little I think I have found a more elegant solution.

I'm saying more elegant since it would leave the ContentPresenter intact, and apply the foreground and fontweight setters to the ContentPresenter's TextElement (which is basically an attached property, but that's beside the point).

The main advantage of this approach is that replacing the ContentPresenter with a TextBlock implicitly assumes that the header will only contain text, which limits the usability of the workaround and produces a less robust code. Leaving the ContentPresenter in place will allow any content, e.g. images+text.

One more thing you would have to do is name your ContentPresenter:

<Setter Property="Template">     <Setter.Value>         <ControlTemplate TargetType="{x:Type TabItem}">             <Grid>                <Border Name="Border" Padding="5,2">                   <ContentPresenter x:Name="CP" ContentSource="Header"/>                </Border>             </Grid>             <ControlTemplate.Triggers>                <MultiTrigger>                   <MultiTrigger.Conditions>                      <Condition Property="Border.IsMouseOver" Value="True"/>                      <Condition Property="IsSelected" Value="False"/>                   </MultiTrigger.Conditions>                     <Setter Property="TextElement.FontWeight" TargetName="CP" Value="Bold"/>                     <Setter Property="TextElement.Foreground" TargetName="CP" Value="Black"/>                                                </MultiTrigger>...

Now the Foreground and FontWeight will not be inherited by the contents of the TabItem (tested).

Enjoy :)


This is pretty old, but I came across it while looking for an answer for a similar problem, and I found the supplied answers not at all helpful.Here is how I fixed this.

If you change the ContentPresenter to a TextBlock in the control template of your tabitem, like this:

....stuff above here...<ControlTemplate TargetType="{x:Type TabItem}"><Grid>   <Border Name="Border" Padding="5,2">        <TextBlock x:Name="TabItemContent" Text="{TemplateBinding Header}"/>   </Border></Grid>... stuff below here....

Then in your trigger on that control template you specify the targetname in the IsSelected trigger..ie.

...stuff above here... <Trigger Property="IsSelected" Value="True">     <Setter Property="Foreground" TargetName="TabItemContent" Value="Green"/>     <Setter Property="FontWeight" Value="Bold"/></Trigger>... stuff below here ...

That should give you green text when the tab is selected, and not green all the other times, while leaving the text coloring in the rest of the app alone.


Unfortunately, if you set the Foreground (or FontWeight) on a ContentPresenter by some trigger, you still assume that the header will only contain Text.

If you set Header="SomeHeaderName" (i.e. text only), the ContentPresenter will generate a TextBlock to host this headertext; the ContentPresenter will be the (logical) parent of this TextBlock and so, the new Foreground set on the ContentPresenter will be inherited by this TextBlock. This works ok.

However, if the Header is assigned some piece of visual tree, like a horizontal StackPanel with an Image and a TextBlock (or even a single TextBlock), then the logical parent of the StackPanel is the TabItem and not the ContentPresenter. Inheritance works via the logical tree and so the TextBlock inside the StackPanel will ultimately inherit its Foreground from the TabItem again; the foreground that was set on the ContentPresenter does not have any effect on this.

One possible solution: a DataTemplate is eventually applied to a ContentPresenter, so the TemplatedParent of the root of the DataTemplate is the ContentPresenter; and inheritance works through a TemplatedParent-Child barrier as well. So if you are able to set the TabItem.HeaderTemplate instead of the TabItem.Header, then you can set the Foreground on the header's ContentPresenter, because the root of the HeaderTemplate will inherit the Foreground from the ContentPresenter. The SelectedContent however, will not because the Foreground is not set on the TabItem (and the content inherits its Foreground from the TabItem).

Hope this helps!