更改标签控制面板背景的颜色

时间:2018-06-21 17:58:10

标签: c# wpf tabcontrol

我有一个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的颜色:

enter image description here

谢谢您的任何建议。

enter image description here

1 个答案:

答案 0 :(得分:1)

OP明确说明后的修改。

您所指的是与TabItem甚至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>

这就是设计器中的样子:

enter image description here

因此,回到您的示例,假设您要设置箭头所指区域的颜色,同时将表单的其余部分保持不同的颜色。为此,我将使用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>

它看起来像这样:

enter image description here