Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>

alt text

like image 893
hafhadg3 Avatar asked Dec 26 '10 03:12

hafhadg3


2 Answers

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.

like image 64
Rick Sladkey Avatar answered Sep 21 '22 18:09

Rick Sladkey


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:

enter image description here

<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>

like image 42
Scott Solmer Avatar answered Sep 19 '22 18:09

Scott Solmer