How to change the background of tabitem by using trigger in xaml? - wpf

Here is my Xaml code
<Window.Resources>
<Style x:Key="SimpleTabControl" TargetType="{x:Type TabItem}">
<!--<Setter Property = "TabStripPlacement" Value = "Top"/>-->
<Setter Property = "Background" Value= "Gray"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Width" Value="200"/>
<Setter Property="Height" Value="40"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="IsSelected" Value="False"/>
<Setter Property="Margin" Value="3,0"/>
<Style.Triggers>
<Trigger Property="TabItem.IsMouseOver" Value="true">
<Setter Property="Background" Value="LightGreen"></Setter>
</Trigger>
</Style.Triggers>
</Style>
<EventTrigger RoutedEvent="MouseEnter">
<EventTrigger.Actions>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type TabPanel}">
<Setter Property="Background" Value="LightGreen"></Setter>
</Style>
</Window.Resources>
<Grid Background="White" Height="1000" Width="auto" IsEnabled="True">
<TabControl Name="MainTab" Background="LightGray" SelectionChanged="MainTab_SelectionChanged">
<TabControl.Resources>
<Style TargetType="{x:Type TabPanel}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Background" Value="White"/>
</Style>
</TabControl.Resources>
<TabItem Header="Input" x:Name="tabAlert1" Style="{StaticResource SimpleTabControl}" MouseEnter="TabItem_Enter" MouseLeave="TabItem_Leave" MouseDoubleClick="TabItem_Click" >
<!--<TabItem.Header>
<Border Padding="0,0">
<StackPanel Orientation="Horizontal" Background="Gray" HorizontalAlignment="Left" Width="200" Height="40" Margin="0,0">
<TextBlock Text="Input" Foreground="White" FontSize="15" TextAlignment="Right"/>
</StackPanel>
</Border>
</TabItem.Header>-->
<Label Content="Content goes here..." />
</TabItem>
<TabItem Header="Analysis" x:Name="tabAlert2" Style="{StaticResource SimpleTabControl}" />
<TabItem Header="Action???" x:Name="tabAlert3" Style="{StaticResource SimpleTabControl}"/>
</TabControl>
</Grid>
I don't know for some reason the trigger for isMouseOver is not triggering the back ground color change Change for Tab Item.
I have also tried the c# , code behind way , but with no success, but if change the foreground property, it works, but not he Background property.
Here is my C# Way
private void TabItem_Enter(object sender, System.Windows.Input.MouseEventArgs e)
{
//var tabItem = sender as TabItem;
//tabAlert2.Background = Brushes.Red;
//tabItem.Background = Brushes.Green;
var tabItem = sender as TabItem;
tabItem.Background = new SolidColorBrush(Colors.Gray);
}
I have Bound the mouse enter event to tab Item.this is very basic but am frustrated this not working?Any help much appreciated.

I'm not sure but I think you want to change the color of the Header?
If so, you can change the header template, try this code and if it works for you, add your other properties.
<Window.Resources>
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid Name="Panel">
<Border Name="Border" BorderThickness="1,1,1,0" BorderBrush="Gainsboro" CornerRadius="4,4,0,0" Margin="2,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="10,2"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="LightGreen"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="Border" Property="Background" Value="White" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="LightBlue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TabControl Name="MainTab">
<TabItem Header="Input" x:Name="tabAlert1"/>
<TabItem Header="Analysis" x:Name="tabAlert2"/>
<TabItem Header="Action???" x:Name="tabAlert3"/>
</TabControl>
</Grid>
(Mouse is over second tab)

Related

How to style tabitem same same height level top?

I custom to stlye tab item. but the first child dont same level with content area. picture. How to fix this. default tabcontrol thers border around border around tabitem.
<Application.Resources>
<Style x:Key="BlueOcean" TargetType="{x:Type TabControl}">
<Style.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Height" Value="50" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<StackPanel Name="Panel">
<ContentPresenter
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Panel" Property="Background" Value="LightSkyBlue" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Panel" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
</Application.Resources>
TabControl View
<TabControl Style="{DynamicResource BlueOcean}" TabStripPlacement="Left">
<TabItem Header="Home" Content="Home Page" />
<TabItem Header="Settings" Content="Settings Page" />
</TabControl>

Disable border and background change on TabItem selected wpf

In my WPF document I have this:
<Window.Resources>
<Style x:Key="SidebarTab" TargetType="TabItem">
<Setter Property="Foreground" Value="#303030"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="Background" Value="#151515"></Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{StaticResource PrimaryHueMidBrush}"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="Background" Value="#151515"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Border Background="#151515" CornerRadius="20">
<Grid Margin="23,47,0,0">
<TabControl TabStripPlacement="Left" Style="{DynamicResource SidebarTabControl}">
<TabItem Style="{DynamicResource SidebarTab}">
<TabItem.Header>
<materialDesign:PackIcon Kind="EmoticonLol" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</TabItem.Header>
</TabItem>
<TabItem Style="{DynamicResource SidebarTab}">
<TabItem.Header>
<materialDesign:PackIcon Kind="EmoticonLol" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</TabItem.Header>
</TabItem>
<TabItem Style="{DynamicResource SidebarTab}">
<TabItem.Header>
<materialDesign:PackIcon Kind="EmoticonLol" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</TabItem.Header>
</TabItem>
<TabItem Style="{DynamicResource SidebarTab}">
<TabItem.Header>
<materialDesign:PackIcon Kind="EmoticonLol" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</TabItem.Header>
</TabItem>
<TabItem Style="{DynamicResource SidebarTab}">
<TabItem.Header>
<materialDesign:PackIcon Kind="EmoticonLol" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</TabItem.Header>
</TabItem>
</TabControl>
</Grid>
</Border>
And it looks like this:
I get the desired effect of the icon changing color on the TabItem, but the Background and BorderBrush (as well as BorderThickness) are still being changed to kind of "link up" to the content inside the header and this is what I want to remove:
I added the things inside the style like so:
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{StaticResource PrimaryHueMidBrush}"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="Background" Value="#151515"></Setter>
</Trigger>
</Style.Triggers>
But it still doesn't work. Does anyone have any suggestions on how to do this?
I tried with your styles and I am also able to get the border. The reason for this is actually the default Template of TabItem has the border color and thickness which your properties when you define your colors still does not get applied to them.
So what you need to do is actually to re-define the control template for your tab item.
I have made basic changes by overriding the Template property of TabItem and added some stuff and bind the BorderThickness, BorderBrush and Background.
<Style x:Key="SidebarTab" TargetType="TabItem">
<Setter Property="Foreground" Value="#303030"></Setter>
<Setter Property="BorderBrush" Value="Red"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="Background" Value="White"></Setter>
<!--I have added this START-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter ContentSource="Header" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<!--I have added this END-->
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Blue"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="Background" Value="Yellow"></Setter>
</Trigger>
</Style.Triggers>
</Style>
Note:- I have change the property values of Foreground, Background, Border thickness in the above code for test purpose and as I could not find the static resources. You have to change back to your keys.
I hope I have made a point for you to proceed further. Give a try and if you find any issues let me know.

Changing the text color of the selected tabItem in a TabControl?

In the second code there is a textBlock with the text "Mina övningar"
How can I change the text color to black when the tabItem is selected?
style:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Margin="0,0,0,13" CornerRadius="5" >
<ContentPresenter x:Name="ContentSite" VerticalAlignment="Top" HorizontalAlignment="Center" ContentSource="Header" Margin="9"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black"/>
<Setter TargetName="Border" Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFF9F7FD" Offset="0.432" />
<GradientStop Color="#FFECF7FD" Offset="0.433" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter TargetName="ContentSite" Property="Margin" Value="9,12,9,9" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="Transparent" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
tabitem:
<TabItem Background="White">
<TabItem.Header>
<StackPanel Orientation="Vertical">
<Image Height="32" Source="/Glosboken;component/Images/library bookmarked.png" />
<TextBlock Text="Mina övningar" Margin="0,0,0,0" VerticalAlignment="Center" Foreground="White" />
</StackPanel>
</TabItem.Header>
<Grid>
<ListBox Height="216" Name="bookslist" VerticalAlignment="Top" Background="White" BorderBrush="White" Foreground="White" SelectedIndex="0" Margin="0,0,129,0" />
</Grid>
</TabItem>
One solution is to use a separate style for this situation:
<Style x:Key="TabItemText" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TabItem}}" Value="True">
<Setter Property="Foreground" Value="Black"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TabItem}}" Value="False">
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
and then replace:
Foreground="White"
with:
Style="{StaticResource TabItemText}"
in the TextBlock.
I did this by naming the ContentPresenter and targeting it in the trigger.
This way it keeps everything for the TabItem style in one place.
Complete Example:
<Window x:Class="TabControlText.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="Border" BorderThickness="1,1,1,0" CornerRadius="5,5,0,0"
Padding="25,5,25,5" Margin="0,0,0,0" BorderBrush="Gainsboro">
<ContentPresenter x:Name="ContentSite" ContentSource="Header" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="TextElement.Foreground" TargetName="ContentSite" Value="White"/>
<Setter TargetName="Border" Property="Background" Value="Black"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="TextElement.Foreground" TargetName="ContentSite" Value="Black"/>
<Setter TargetName="Border" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TabControl>
<TabItem Header="Tab 1" Style="{DynamicResource TabItemStyle1}" />
<TabItem Header="Tab 2" Style="{DynamicResource TabItemStyle1}" />
<TabItem Header="Tab 3" Style="{DynamicResource TabItemStyle1}" />
<TabItem Header="Tab 4" Style="{DynamicResource TabItemStyle1}" />
</TabControl>
</Grid>
I extended Scott Solmer's great code by making it a resource dictionary because I needed this TabItem styling application wide.
So add new TabItemStyles.xaml under the Resources Dictionary folder which was called "Resources" :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ColoredTabsStyle" TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="Border" BorderThickness="1,1,1,0" CornerRadius="5,5,0,0"
Padding="25,5,25,5" Margin="0,0,0,0" BorderBrush="Gainsboro">
<ContentPresenter x:Name="ContentSite" ContentSource="Header" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="TextElement.Foreground" TargetName="ContentSite" Value="White" />
<Setter TargetName="Border" Property="Background" Value="Black" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="TextElement.Foreground" TargetName="ContentSite" Value="Black" />
<Setter TargetName="Border" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I changed my app.xaml to know about the new resource dictionary. WARNING - adjust the source and component locations for your application:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/TruPredict;component/Resources/TabItemStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
Then I used style in the actual screen everywhere in the application I needed. Unlike Scott, I prefer using StaticResource rather than DynamicResource unless I have to.:
<TabControl>
<TabItem Header="Tab1" Style="{StaticResource ColoredTabsStyle}">
<TabItem Header="Tab2" Style="{StaticResource ColoredTabsStyle}">
<TabItem Header="Tab3" Style="{StaticResource ColoredTabsStyle}">
<TabControl>

WPF: Setting Foreground of a Label inside a TabItem Header using styles

I have a TabControl which looks like this:
<TabControl>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Canvas ... />
<Label>Tab Number 1</Label>
</StackPanel>
</TabItem.Header>
</TabItem>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Canvas ... />
<Label>Tab Number 2</Label>
</StackPanel>
</TabItem.Header>
</TabItem>
</TabControl>
Ok, i like to have a different Text Color when the item is selected. I created a Style for that purpose:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Height="60">
<Border x:Name="Border" BorderBrush="#FFC6C7C8"
BorderThickness="1,0,1,0" Margin="-1,0,0,0">
</Border>
<Border x:Name="BorderOverlay" BorderBrush="Transparent"
BorderThickness="1,0,1,0" Margin="-1,0,0,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="15,6,15,6">
</ContentPresenter>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<!-- What goes here? -->
</Trigger>
<ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I tried by just setting Label.Foreground, but it doesn't seem to work. I also tried it with a TextBlock, which did not worked as well.
I think this question is similar to mine, but the problem wasn't solved in the end:
Setting TabItem foreground color also sets the TabControl foreground color
Thanks for any help.
Try using Style Triggers instead of Control Template Triggers.
Add this to your current style:
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Green" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
Here's a generic style for everyone else.
<Style TargetType="{x:Type TabItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Green" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>

How Do I Change The Text Foreground of a WPF TabItem With Nested Text?

I'm looking to have the foreground of the text of a TabItem change whenever the tab becomes active. I was using the following, which was working fine until I changed the type of content being displayed in the tab:
<TabControl Style="{DynamicResource SidebarTabControl}">
<TabItem Header="TabItem" Style="{DynamicResource SidebarTab}" />
</TabControl>
<Style x:Key="SidebarTabForegroundStyleSelected">
<Setter Property="TextBlock.Foreground" Value="White" />
</Style>
<Style x:Key="SidebarTabForegroundStyle">
<Setter Property="TextBlock.Foreground" Value="Black" />
</Style>
<Style x:Key="SidebarTab" TargetType="TabItem">
<Setter Property="Padding" Value="10,12,2,12" />
<Setter Property="BorderThickness" Value="0,1,0,1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border Padding="{TemplateBinding Padding}"
Name="tab"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{StaticResource SidebarTabBorderBrush}"
SnapsToDevicePixels="True">
<ContentPresenter Style="{StaticResource SidebarTabForegroundStyle}" Name="content" ContentSource="Header" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="tab" Property="Background" Value="{StaticResource SidebarTabBackgroundBrushSelected}" />
<Setter TargetName="tab" Property="BorderBrush" Value="{StaticResource SidebarTabBorderBrushSelected}" />
<Setter TargetName="content" Property="Style" Value="{StaticResource SidebarTabForegroundStyleSelected}" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="tab" Property="Background" Value="{StaticResource SidebarTabBackgroundBrush}" />
<Setter TargetName="tab" Property="BorderBrush" Value="{StaticResource SidebarTabBorderBrush}" />
<Setter TargetName="content" Property="Style" Value="{StaticResource SidebarTabForegroundStyle}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
When I changed the TabItem to:
<TabControl Style="{DynamicResource SidebarTabControl}">
<TabItem Style="{DynamicResource SidebarTab}">
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image Height="16" Source="..\..\Icons\cog.png" />
<TextBlock Text="TabItem" Margin="5,0,0,0" VerticalAlignment="Center" />
</StackPanel>
</TabItem.Header>
</TabItem>
</TabControl>
The foreground of the text no longer turns to white when the tab is selected and back to black when the tab is no longer selected. Everything else still works correctly.
Does anyone know if there is a way to accomplish changing the color of the foreground in the XAML above?
Move the trigger from the control template to the style:
<Grid>
<Grid.Resources>
<SolidColorBrush x:Key="SidebarTabBackgroundBrushSelected" Color="Gray"></SolidColorBrush>
<SolidColorBrush x:Key="SidebarTabBorderBrushSelected" Color="Blue"></SolidColorBrush>
<SolidColorBrush x:Key="SidebarTabBackgroundBrush" Color="LightGray"></SolidColorBrush>
<SolidColorBrush x:Key="SidebarTabBorderBrush" Color="Green"></SolidColorBrush>
<Style x:Key="SidebarTab" TargetType="TabItem">
<Setter Property="Padding" Value="10,12,2,12" />
<Setter Property="BorderThickness" Value="0,1,0,1" />
<Setter Property="Foreground" Value="Blue"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border Padding="{TemplateBinding Padding}"
Name="tab"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{StaticResource SidebarTabBorderBrush}"
SnapsToDevicePixels="True">
<ContentPresenter Name="content"
ContentSource="Header" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="tab" Property="Background" Value="{StaticResource SidebarTabBackgroundBrushSelected}" />
<Setter TargetName="tab" Property="BorderBrush" Value="{StaticResource SidebarTabBorderBrushSelected}" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="tab" Property="Background" Value="{StaticResource SidebarTabBackgroundBrush}" />
<Setter TargetName="tab" Property="BorderBrush" Value="{StaticResource SidebarTabBorderBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<TabControl Style="{DynamicResource SidebarTabControl}">
<TabItem Header="TabItem 1" Style="{DynamicResource SidebarTab}" />
<TabItem Style="{DynamicResource SidebarTab}" >
<TabItem.Header>
<StackPanel>
<TextBlock Text="a"></TextBlock>
<TextBlock Text="b"></TextBlock>
</StackPanel>
</TabItem.Header>
</TabItem>
<TabItem Header="TabItem 3" Style="{DynamicResource SidebarTab}" />
</TabControl>
</Grid>
It looks like the issue is coming up because you are setting the wrong property:
<Style x:Key="SidebarTabForegroundStyleSelected">
<Setter Property="TextBox.Foreground" Value="White" />
</Style>
<Style x:Key="SidebarTabForegroundStyle">
<Setter Property="TextBox.Foreground" Value="Black" />
</Style>
You need to be setting TextElement.Foreground or TextBlock.Foreground
Also, since it is an inherited property, you can just set the AttachedProperty directly on the TabItems, you don't need to assign it specifically to the content.
<TabControl Style="{DynamicResource SidebarTabControl}">
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="TextElement.Foreground"
Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TabControl.ItemContainerStyle>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image Height="16"
Source="..\..\Icons\cog.png" />
<TextBlock Text="TabItem"
Margin="5,0,0,0"
VerticalAlignment="Center" />
</StackPanel>
</TabItem.Header>
Item 1
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock Text="Tab 2" />
</TabItem.Header>
Item 2
</TabItem>
<TabItem Header="Item 3">
Item 3
</TabItem>
</TabControl>

Resources