1

我有一个TabControlTabItems 的,TabItems 有文本和一个图标。为此,我必须StackPanelTabItem.

但是,一旦添加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.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"不再起作用。我可以看到原因,但无法找出其他方法。有任何想法吗?

4

1 回答 1

0

只需在实际的TabItem. 例如,请注意您是否将以下内容放在样式的顶部:

<Setter Property="Foreground" Value="Red" />

文本仍然改变。你不需要引用ContentPresenter's TextBlock,即使你这样做了,你也可以应用TemplateBindings到它的属性上,这样当TabItem's 的属性改变时,'s也会改变ContentPresenter

于 2014-06-05T20:38:03.550 回答