在XAML TabControl中,如何设置焦点选项卡标题的样式?

时间:2009-04-08 12:32:10

标签: xaml tabcontrol

我可以使用TabItem.Background设置每个TabItem的背景,但是当该选项卡选择时,它是纯白色的。

如何设置关注的标题页的样式

<TabControl DockPanel.Dock="Top">
    <TabControl.Background>
        <LinearGradientBrush EndPoint="1.115,1.13" StartPoint="0,-0.02">
            <GradientStop Color="#FFFFFFFF" Offset="1"/>
            <GradientStop Color="#FFE0E376" Offset="0"/>
        </LinearGradientBrush>
    </TabControl.Background>

    <TabItem Header="Allgem." Cursor="Hand">
        <TabItem.Background>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFF3F3F3" Offset="0"/>
                <GradientStop Color="#FFF11818" Offset="1"/>
            </LinearGradientBrush>
        </TabItem.Background>
        <StackPanel DockPanel.Dock="Bottom" Width="400" HorizontalAlignment="Left" Margin="10">
            ...

1 个答案:

答案 0 :(得分:5)

您可以使用触发器仅更改所选标签的样式:

<TabControl DockPanel.Dock="Top">
    <TabControl.Resources>
        <Style TargetType="{x:Type TabItem}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                <GradientStop Color="#FFF3F3F3" Offset="0"/>
                                <GradientStop Color="#FFF11818" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TabControl.Resources>
    <TabControl.Background>
        <LinearGradientBrush EndPoint="1.115,1.13" StartPoint="0,-0.02">
            <GradientStop Color="#FFFFFFFF" Offset="1"/>
            <GradientStop Color="#FFE0E376" Offset="0"/>
        </LinearGradientBrush>
    </TabControl.Background>
    <TabItem Header="Allgem." Cursor="Hand">
        <StackPanel DockPanel.Dock="Bottom" Width="400"
            HorizontalAlignment="Left" Margin="10">
        ...
        </StackPanel>
    </TabItem>
</TabControl>

这会将所选标签的背景设置为示例代码中使用的红色渐变。