我正在重新设置 TabItem 的样式,并希望 TabItem Header 的背景、前景和文本颜色根据是否选择而有所不同。我可以在 ControlTemplate.Triggers 之外成功更改 ContentPresenter 的 TextBlock,但我不确定如何从 Trigger 中“获取”它。
这是我的 XAML:
<Style x:Key="SiteTabItemStyle" d:IsControlPart="True" TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Margin="0,0,-4,0" x:Name="Border" BorderThickness="2" CornerRadius="15,15,0,0" BorderBrush="Black" Background="DimGray">
<ContentPresenter TextBlock.Foreground="LightGray" HorizontalAlignment="Center" Margin="12,2,12,2" x:Name="ContentSite" VerticalAlignment="Center" RecognizesAccessKey="True" ContentSource="Header"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="100"/>
<Setter Property="Background" Value="Gold" TargetName="Border"/>
<Setter Property="BorderThickness" Value="2" TargetName="Border"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
<Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border"/>
<Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>