在TabControl WPF中更改TabItem的默认突出显示颜色

时间:2010-10-08 10:38:42

标签: c# .net wpf xaml tabcontrol

我试图更改WPF中标签控件中标签项的默认突出显示颜色。

在图像中,高光颜色为橙色,我只想知道是否有将其更改为另一种纯色?

alt text

这是我的XAML,它声明了TabControl和2个TabItems

<TabControl>
            <TabControl.Background>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FFCCCCD0"/>
                    <GradientStop Color="#FF526593" Offset="1"/>
                </LinearGradientBrush>
            </TabControl.Background>
            <TabItem Header="test1">
                <TabItem.Content>
                    <StackPanel Orientation="Horizontal">
                        <Button Content="Test" VerticalAlignment="Center" />
                        <Button Content="Test2" />
                    </StackPanel>
                </TabItem.Content>              
            </TabItem>
            <TabItem Header="Test2">
                <TabItem.Content>
                    <TextBox />
                </TabItem.Content>
            </TabItem>
        </TabControl>

请注意我也无法访问表达式混合,因此在Visual Studio 2010中需要任何解决方案。

感谢。

1 个答案:

答案 0 :(得分:4)

您需要覆盖TabItem控件的样式。 下面是一个仍需要一些调整的例子。只需将其更改为您想要的样式即可。 选中时,IsSelected触发器会对TabItem添加更改。

<Window.Resources>
    <ResourceDictionary>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="BorderThickness"
                    Value="3" />
            <Setter Property="BorderBrush"
                    Value="Blue" />
            <Setter Property="VerticalContentAlignment"
                    Value="Center" />
            <Setter Property="HorizontalContentAlignment"
                    Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Border>
                            <Grid>
                                <Grid>
                                    <Border x:Name="border" 
                                            CornerRadius="3,3,0,0"
                                            Background="{TemplateBinding Background}"
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            BorderThickness="1,1,1,0" />
                                </Grid>
                                <Border BorderThickness="{TemplateBinding BorderThickness}"
                                        Padding="{TemplateBinding Padding}">
                                    <ContentPresenter ContentSource="Header"
                                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </Border>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"
                                     Value="True">
                                <Setter TargetName="border"
                                        Property="BorderBrush"
                                        Value="Red" />
                                <Setter TargetName="border"
                                        Property="BorderThickness"
                                        Value="0,3,0,0" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Window.Resources>

<Grid>
    <TabControl>
        <TabControl.Background>
            <LinearGradientBrush EndPoint="0,1"
                                 StartPoint="0,0">
                <GradientStop Color="#FFCCCCD0" />
                <GradientStop Color="#FF526593"
                              Offset="1" />
            </LinearGradientBrush>
        </TabControl.Background>
        <TabItem Header="test1">
            <TabItem.Content>
                <StackPanel Orientation="Horizontal">
                    <Button Content="Test"
                            VerticalAlignment="Center" />
                    <Button Content="Test2" />
                </StackPanel>
            </TabItem.Content>
        </TabItem>
        <TabItem Header="Test2">
            <TabItem.Content>
                <TextBox />
            </TabItem.Content>
        </TabItem>
    </TabControl>
</Grid>