设置选项卡被点击和未被点击时 TabItem header 的前景颜色

Setting the color of the TabItem header's foreground when the tab is clicked and not clicked

我的应用程序中有这个 TabControl

<TabControl Width="100" Height="100">
    <TabControl.Resources>
        <Style TargetType="{x:Type TabItem}" x:Key="TabItemStyle">
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Border x:Name="Chrome"
                                BorderBrush="Blue" 
                                Background="Transparent">
                            <ContentPresenter ContentSource="Header"
                                              Margin="0,0,0,3"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Selector.IsSelected" Value="True">
                                <Setter TargetName="Chrome" Property="BorderThickness" Value="0,0,0,3"/>
                            </Trigger>
                            <Trigger Property="Selector.IsSelected" Value="False">
                                <Setter TargetName="Chrome" Property="BorderThickness" Value="0,0,0,0"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>

    <TabItem Style="{DynamicResource TabItemStyle}">
        <TabItem.Header>
             <Label Content="Tab 1" />
        </TabItem.Header>
    </TabItem>

    <TabItem Style="{DynamicResource TabItemStyle}">
        <TabItem.Header>
             <Label Content="Tab 2" />
        </TabItem.Header>
    </TabItem>
</TabControl>

当我在TabControl中点击一个标签时,标签下方会显示一条蓝线header。
这是题外话,但我确定样式我有会有用。

标签 headers 中有标签。
我想根据以下内容更改标签的前景:

我想我可以使用 ControlTemplate 中已有的触发器来确定选项卡何时被单击。

但我不知道我需要做什么才能在样式中相应地设置标签前景。

您可以创建一个 DataTemplate as HeaderTemplate and use a DataTrigger that sets the Foreground color of the Labels depending on the selection state of the containing TabItems, which is bound using a RelativeSource。单独的 header 模板还减少了 TabItem 中的冗余,因为您不必每次都再次定义 Label

<TabControl Width="100"
            Height="100">
   <TabControl.Resources>
      <Style TargetType="{x:Type TabItem}"
             x:Key="TabItemStyle">
         <Setter Property="VerticalContentAlignment"
                 Value="Stretch" />
         <Setter Property="HorizontalContentAlignment"
                 Value="Stretch" />
         <Setter Property="HeaderTemplate">
            <Setter.Value>
               <DataTemplate>
                  <Label x:Name="TabItemHeaderLabel" Foreground="Gray" Content="{Binding}" />
                  <DataTemplate.Triggers>
                     <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" Value="True">
                        <Setter TargetName="TabItemHeaderLabel" Property="Foreground" Value="Red"/>
                     </DataTrigger>
                  </DataTemplate.Triggers>
               </DataTemplate>
            </Setter.Value>
         </Setter>
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type TabItem}">
                  <Border x:Name="Chrome"
                          BorderBrush="Blue"
                          Background="Transparent">
                     <ContentPresenter ContentSource="Header"
                                       Margin="0,0,0,3"
                                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                  </Border>
                  <ControlTemplate.Triggers>
                     <Trigger Property="Selector.IsSelected"
                              Value="True">
                        <Setter TargetName="Chrome"
                                Property="BorderThickness"
                                Value="0,0,0,3" />
                     </Trigger>
                     <Trigger Property="Selector.IsSelected"
                              Value="False">
                        <Setter TargetName="Chrome"
                                Property="BorderThickness"
                                Value="0,0,0,0" />
                     </Trigger>
                  </ControlTemplate.Triggers>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
   </TabControl.Resources>

   <TabItem Style="{DynamicResource TabItemStyle}"
            Header="Tab 1"/>

   <TabItem Style="{DynamicResource TabItemStyle}"
            Header="Tab 2"/>
</TabControl>