更改 TabItem 的 Header 字体粗细

Change TabItem's Header FontWeight

我想将 TabItemHeader FontWeight 更改为 BoldIsSelected = True 来自我的 ResourceDictionary 时。

一些代码

 <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid>
                    <Border Name="Border"
                            Background="White"
                            BorderBrush="White" 
                            BorderThickness="1,1,1,0"
                            Margin="0,0,0,-1" >
                        <ContentPresenter x:Name="ContentSite"
                                          Height="25"
                                          VerticalAlignment="Bottom"
                                          HorizontalAlignment="Center"
                                          ContentSource="Header"
                                          Margin="12,2,12,2"/>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="#F2F1F0" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="Gray" />
                        <Setter TargetName="Border" Property="BorderThickness" Value="0.5,0.7,0.5,0" />
                        <Setter Property="Header">
                            <Setter.Value>
                                <TextBlock Text="{Binding Path=Header.Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}"
                                           FontWeight="Bold" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="White" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="Gray" />
                        <Setter TargetName="Border" Property="BorderThickness" Value="0,0,0,0.5" />
                    </Trigger>

.....etc.....

上面的方法是行不通的。还设置 HeaderTemplate 而不是 Header 它可以工作,但它会破坏我的模板。最后,将 TextBlock.FontWeight 设置为 Trigger 内的 Property 会更改 TabItem.

内的所有 TextBlock

有什么建议吗?

更新

我选择了@ShineKing 的回答,因为它帮助我解决了我的问题。基本上我所做的是将 DataTrigger 添加到 TextBlock 用作 Header 到我的 "custom" TabItem.

            <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" Value="True">
                        <Setter Property="FontWeight" Value="Bold"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>

您能否尝试使用此代码将 TextBlock 更改为粗体

<TabControl>
   <TabControl.Resources>
      <Style TargetType="TextBlock" x:Key="HeaderTextBlockStyle">
         <Style.Triggers>
            <DataTrigger Binding="{Binding IsSelected,
                          RelativeSource={RelativeSource AncestorType=TabItem}}"
                         Value="True">
                <Setter Property="FontWeight" Value="Bold"/>
             </DataTrigger>
          </Style.Triggers>
       </Style>
    </TabControl.Resources>
    <TabItem>
       <TabItem.Header>
          <TextBlock Text="Header Text"
                     Style="{StaticResource HeaderTextBlockStyle}"/>
       </TabItem.Header>
    </TabItem>
</TabControl>