Is there a simple way to set the background brush of all inactive tabs in a WPF TabControl? I want to emulate the look of VS 2010 on a TabControl–the background color of the control's inactive tabs should match the background color of the window in which the TabControl is sited, so that you see only the text of the tab, and not the tab itself.
I know it will take a ControlTemplate to do it; I am trying to figure out what to put in the control template. Put another way, How do I specify that a particular brush should be applied to all inactive tabs? Thanks for your help.
Best Answer
Here is the solution: As Stephen said, add a trigger to the control template. It's actually a property trigger, and it only needs to be set for the inactive state. So we set the trigger for IsSelected = false. We target the border (
Bd
in the default control template for aTabItem
) of theTabItem
and set itsBackground
to the color we want (I useRelativeSource FindAncestor
to match the grid on which the tab is placed). Then we setBd
'sBorderThickness
to 0, and we're done:I put the trigger in the default template, just below the IsSelected = true trigger.
Note that the trigger is hard-coded to search for a
Grid
ancestor as the source of the inactive tab background color (AncestorType={x:Type Grid}
). That's because I set my view background in theGrid
that I use as my layout root. You will need to change theAncestorType
if you use a different layout root control, or if you set your view background color elsewhere (such as in the<Window>
tag).BTW, you can also use the IsSelected = true trigger to change the
Background
of the active tab header from white, to match theTabControl
background color:For those who like this visual representation, here is the complete control template. It will be applied automatically to any
TabControl
within its scope. Simply add this markup to the section of your XAML window (or import it from aResourceDictionary
), and yourTabControl
will get the VS 2010 look. Remember to change theFindAncestor
proeprty so that the template will find the correct background color.