0
votes

I wrote a style for my TabControl. Within the TabControl I have a TextBlock and a Button. I wish to set trigger for TabItem.IsSelected such that the font colour of the text within TextBlock changed. My code below doesn't work:

  <Style x:Key="_tabItemButtonStyle" BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type TabItem}">
      <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Name="_TabHeaderStackPanel" >
                            <TextBlock Text="{ Binding TabName }" Name="_TabHeaderText" Background="{ Binding TabBackColour }" FontSize="{ Binding TabFontSize }" >
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsSelected,
                                    RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                            Value="True">
                                                <Setter Property="Foreground" Value="SteelBlue"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>

                            </TextBlock>
       ...

The problem I suspect is with this code:

                                 <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsSelected,
                                    RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                            Value="True">
                                                <Setter Property="Foreground" Value="SteelBlue"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

EDIT:

I bind my tab items to a Collection of ViewModels. So my style binding looks as following:

 <TabControl x:Name="_MainTabControl"  Grid.Column="1" Grid.Row="1"
                    SelectedIndex="0"
                    ItemsSource="{Binding OpenTabs}"
                    ItemContainerStyle="{StaticResource _tabItemButtonStyle}" />
1
Just move the style with triggers outside of your DataTemplate, it should work then. P.S. place it in your TabControl.Resources. HTHXAMlMAX

1 Answers

0
votes

I don't know what MetroTabItem is. But the other Code looks fine. I tried it out with sample Code. And it does exactly what you describe you want to achieve.

Please check whether your Styles are correctly applied with Style={StaticResource YourStyleName} on the TabItem

Here my XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Grid>
    <Grid.Resources>
        <ResourceDictionary>
    <Style TargetType="{x:Type TabItem}" x:Key="TestStyle">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Name="_TabHeaderStackPanel" >
                        <TextBlock Text="test" Name="_TabHeaderText" Background="{ Binding TabBackColour }" FontSize="{ Binding TabFontSize }" >
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsSelected,
                                RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                        Value="True">
                                            <Setter Property="Foreground" Value="SteelBlue"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>

                        </TextBlock>
                        <Button Content="Test"></Button>
                        </StackPanel></DataTemplate></Setter.Value>

                    </Setter>
            </Style>
        </ResourceDictionary>
    </Grid.Resources>
    <TabControl>
        <TabItem Style="{StaticResource TestStyle}">
        </TabItem>
        <TabItem Style="{StaticResource TestStyle}">

        </TabItem>
    </TabControl>
    </Grid>