首页 > 解决方案 > 更改选项卡控制面板背景的颜色

问题描述

我有一个TabControl这样的:

<TabControl ItemsSource="{Binding TabItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                    SelectedIndex="0" 
                    BorderThickness="0" 
                    TabStripPlacement="Left"
                    Padding="10,0,0,10">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="TabItem">
                                <Grid Name="Panel">
                                    <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="20,5"/>
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="Panel" Property="Background" Value="#d9534f" />
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="Panel" Property="Background" Value="#E6E3DB" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Header" Value="{Binding Header, Mode=OneTime}"/>
                    <Setter Property="Content" Value="{Binding Content, Mode=OneTime}"/>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>

有没有办法改变Panel所在位置的颜色TabItems

在此处输入图像描述

感谢您的任何建议。

在此处输入图像描述

标签: c#wpftabcontrol

解决方案


OP澄清问题后编辑。

你所指的 are 与 theTabItem甚至TabControl. 它只是TabControl驻留的控件。

请看以下简单示例:

<Window x:Class="StackOverflowWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StackOverflowWPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="300">
    <Grid Background="LightSeaGreen">
        <TabControl Background="Red">
            <TabItem Header="Tab1" Background="Yellow">
            </TabItem>
            <TabItem Header="Tab1" Background="Green">
            </TabItem>
        </TabControl>
    </Grid>
</Window>

这就是它在设计器中的样子:

在此处输入图像描述

所以回到你的例子,我假设你想设置箭头指向的区域的颜色,同时保持表单的其余部分为不同的颜色。我会Grid为此使用控件。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" Background="LightGreen">
        <TabControl Grid.Row="0" Background="Red" Height="100">
            <TabItem Header="Tab1" Background="Yellow">
            </TabItem>
            <TabItem Header="Tab1" Background="Green">
            </TabItem>
        </TabControl>
    </Grid>
    <Grid Grid.Row="1"/>
</Grid>

它看起来像这样:

在此处输入图像描述


推荐阅读