如何更改 WPF tabitem onClick 的 header 文本的前景?

How to change the foreground of the header text of a WPF tabitem onClick?

如何在 TabItem header 文本被选中或处于活动状态时仅更改其前景色,而不更改 TabItem 中任何项目或其他内容的前景?我是这一切的新手,所以很难弄清楚我可以在我的 TabItem 样式中的什么地方实现这个小功能以及如何实现。

我使用的 TabControl 样式是:

<Style
    x:Key="{x:Type TabControl}"
    TargetType="{x:Type TabControl}">
    <Setter
        Property="BorderThickness"
        Value="0" />
    <Setter
        Property="BorderBrush"
        Value="Transparent" />
    <Setter
        Property="Background"
        Value="Transparent" />
    <Setter
        Property="Template">
        <Setter.Value>
            <ControlTemplate
                TargetType="TabControl">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition
                            Height="Auto" />
                        <RowDefinition
                            Height="*" />
                    </Grid.RowDefinitions>
                    <Border
                        BorderThickness="0,0,1,1"
                        BorderBrush="#D0CEBF"
                        Grid.Row="1">
                        <Border
                            BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                            <Border
                                Background="{TemplateBinding Background}">
                                <ContentPresenter
                                    ContentSource="SelectedContent" />
                            </Border>
                        </Border>
                    </Border>
                    <TabPanel
                        Grid.Row="0"
                        IsItemsHost="true"
                        HorizontalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style
    TargetType="TabItem">
    <Setter
        Property="BorderThickness"
        Value="0" />
    <Setter
        Property="BorderBrush"
        Value="Transparent" />
    <Setter
        Property="Background"
        Value="Transparent" />
    <Setter
        Property="VerticalContentAlignment"
        Value="Center" />
    <Setter
        Property="HorizontalContentAlignment"
        Value="Center" />
    <Setter
        Property="Template">
        <Setter.Value>
            <ControlTemplate
                TargetType="{x:Type TabItem}">
                <!--In order to click on the blank part of the TabItem can be successfully selected-->
                <Border
                    Background="Transparent">
                    <Grid>
                        <Grid
                            x:Name="g">
                            <Path
                                Fill="{TemplateBinding Background}"
                                Margin="0,0,0,-1"
                                Data="M 20,40 L 0,40 0,40 C 4,40 10,36 10,30 L 10,10 C 10,0 16,0 20,0 L 155,0  C 165,0 173,0 ,175,6 L175,30 C 175,30 175,38 182,40 Z" />
                        </Grid>
                        <Border
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                            <ContentPresenter
                                ContentSource="Header"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                    </Grid>
                </Border>
                <!--Add ControlTemplate.Triggers to change the background color of the selected TabItem.-->
                <ControlTemplate.Triggers>
                    <Trigger
                        Property="IsSelected"
                        Value="false">
                        <Setter
                            Property="Visibility"
                            TargetName="g"
                            Value="Hidden" />
                    </Trigger>
                
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

不幸的是,在您的 TabItem 控件模板中添加触发器也会在选择选项卡时设置 TabItem 内容的前景色,因此这不起作用。

<Trigger Property="IsSelected" Value="True">
   <Setter Property="Foreground" Value="Red" />
</Trigger>

但是,您可以在 ContentPresenter 资源中添加隐式 TextBlock 样式(没有 x:Key)。它将仅自动应用于其范围内的 TextBlocks,不会影响 TabItem 内容,甚至不会影响 TextBlocks 以外的自定义 header 模板中的其他元素。

<ContentPresenter
    ContentSource="Header"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" >
   <ContentPresenter.Resources>
      <Style TargetType="{x:Type TextBlock}">
         <Style.Triggers>
            <DataTrigger
               Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
               Value="true">
               <Setter
                  Property="Foreground"
                  Value="Red" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </ContentPresenter.Resources>
</ContentPresenter>

或者,一个专门的解决方案是创建一个 header 模板并将触发器放在那里。在这种情况下,它不会强制覆盖 header 模板将前景应用于每个 TextBlock.

<Setter
    Property="HeaderTemplate">
   <Setter.Value>
      <DataTemplate>
         <TextBlock Text="{Binding}">
            <TextBlock.Style>
               <Style TargetType="{x:Type TextBlock}">
                  <Style.Triggers>
                     <DataTrigger
                        Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
                        Value="true">
                        <Setter
                           Property="Foreground"
                           Value="Red" />
                     </DataTrigger>
                  </Style.Triggers>
               </Style>
            </TextBlock.Style>
         </TextBlock>
      </DataTemplate>
   </Setter.Value>
</Setter>