我有一个我重新设计的 TabControl。The TabItem has a trigger that is fired when the TabItem is selected that changes the TabItem text to bold and green. 我遇到的问题是选项卡内容中的文本也设置为粗体和绿色。
我可以通过将选项卡内容中的所有控件设置为我想要的颜色和字体粗细来解决这个问题,但我应该这样做吗?因此,我必须确保内容区域中的每个文本块都具有将颜色设置为黑色且字体粗细正常的样式。
如何将 TabItem 的 IsSelected 部分设置为显示绿色,但不保留选项卡的内容?
我试图将 TabControl 的前景设置为黑色,但这不起作用。
您将从下面的代码示例中看到,第一个选项卡上的文本是绿色的,我希望它是黑色的,但没有在选项卡内容中设置每个控件。
下面的代码示例:
<Grid>
<Grid.Resources>
<!-- Tab item -->
<Style TargetType="{x:Type TabItem}">
<Setter Property="FontSize" Value="14"/>
<Setter Property="MinWidth" Value="200"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border" Padding="5,2">
<ContentPresenter ContentSource="Header"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Border.IsMouseOver" Value="True"/>
<Condition Property="IsSelected" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="Black"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Border.IsMouseOver" Value="False"/>
<Condition Property="IsSelected" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="Black" />
</MultiTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Tab control -->
<Style TargetType="{x:Type TabControl}">
<Setter Property="SelectedIndex" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Padding="5" Margin="0,0,5,0" CornerRadius="3">
<StackPanel Orientation="Vertical">
<ScrollViewer VerticalScrollBarVisibility="Auto" FocusVisualStyle="{x:Null}">
<TabPanel IsItemsHost="True"/>
</ScrollViewer>
</StackPanel>
</Border>
<Border Grid.Column="1" BorderBrush="Black" BorderThickness="0">
<ScrollViewer VerticalScrollBarVisibility="Auto" FocusVisualStyle="{x:Null}" Padding="10,0">
<ContentPresenter ContentSource="SelectedContent"/>
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<TabControl Name="tabControl" TabStripPlacement="Left">
<!-- First tab item -->
<TabItem IsSelected="True">
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Profile"/>
</StackPanel>
</TabItem.Header>
<TextBlock Text="Page 1 Sample Text with no foreground set." FontSize="30"/>
</TabItem>
<!-- Second tab item -->
<TabItem IsSelected="True">
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Profile"/>
</StackPanel>
</TabItem.Header>
<TextBlock Text="Page 2 Sample Text with foreground set manually." FontSize="30" Foreground="Red"/>
</TabItem>
</TabControl>
</Grid>