我有TabControl
TabItem
个,TabItem
有文字和图标。为此,我必须向StackPanel
添加TabItem
。
但是,一旦我添加StackPanel
,我就无法再控制文本的默认样式。
资源:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Name="tabItem">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="1" Name="tabItemStyle" Background="Transparent" BorderBrush="Transparent" BorderThickness="1,1,1,0" CornerRadius="3,3,0,0" SnapsToDevicePixels="True" Margin="0, 0, 5, 0">
<ContentPresenter x:Name="ContentSite" TextBlock.Foreground="White" TextBlock.FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="14,3,18,3">
</ContentPresenter>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="tabItemStyle" Property="Background" Value="#ecf3f9" />
<Setter TargetName="tabItemStyle" Property="BorderBrush" Value="#29458e" />
<Setter TargetName="tabItemStyle" Property="BorderThickness" Value="1,1,1,0" />
<Setter TargetName="ContentSite" Property="TextBlock.Foreground" Value="#29458e" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="False" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter TargetName="tabItemStyle" Property="Background" Value="#6381be" />
<Setter TargetName="tabItemStyle" Property="BorderBrush" Value="#97acd4" />
<Setter TargetName="tabItemStyle" Property="BorderThickness" Value="1,1,1,0" />
</MultiTrigger.Setters>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
TabItem的:
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Source="/images/icons/_24/reports.png" Width="24" />
<TextBlock VerticalAlignment="Center" Margin="5, 0" >Reports</TextBlock>
</StackPanel>
</TabItem.Header>
</TabItem>
相关行是资源(样式)中的ContentPresenter
。 TextBlock.Foreground="White"
不再有效。我可以看到为什么,但不知道如何做到这一点。有什么想法吗?
答案 0 :(得分:0)
只需在实际TabItem
上设置相关属性即可。例如,请注意如果您将以下内容放在样式的顶部:
<Setter Property="Foreground" Value="Red" />
文字仍然有变化。您不需要引用ContentPresenter
TextBlock
,即使您这样做,也可以将TemplateBindings
应用于其属性,以便{{1} }}的属性已更改,TabItem
&#39>也是如此。