简体   繁体   English

如何更改 TabControl.Header 的背景颜色

[英]How to change background color of TabControl.Header

I need to change the background color of the TabControl header, but TabControl haven't property for it, how can I do it.我需要更改 TabControl 标题的背景颜色,但 TabControl 没有它的属性,我该怎么做。 Help me please.请帮帮我。 Here is my code:这是我的代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="502" Width="628">
    <TabControl Background="#123" TabStripPlacement="Left" HorizontalAlignment="Stretch" BorderBrush="#41020202">
        <TabControl.BitmapEffect>
            <DropShadowBitmapEffect Color="Black" Direction="270"/>
        </TabControl.BitmapEffect>
        <TabControl.Resources>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Padding" Value="0" />
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border x:Name="grid" Background="Red">
                                <ContentPresenter>
                                    <ContentPresenter.Content>
                                        <TextBlock Margin="4" FontSize="15" Text="{TemplateBinding Content}"/>
                                    </ContentPresenter.Content>
                                    <ContentPresenter.LayoutTransform>
                                        <RotateTransform Angle="270" />
                                    </ContentPresenter.LayoutTransform>
                                </ContentPresenter>
                            </Border>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TabItem}},Path=IsSelected}" Value="True">
                                    <Setter TargetName="grid" Property="Background" Value="Green"/>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
        <TabItem Header="Tab Item 1" />
        <TabItem Header="Tab Item 2" />
        <TabItem Header="Tab Item 3" />
        <TabItem Header="Tab Item 4" />
    </TabControl>
</Window>

Here is my result:这是我的结果:我的结果

Here is result that I need:这是我需要的结果:这是我需要的结果

Adding the following style in the TabControl.Resources section should do what you want:TabControl.Resources部分中添加以下样式应该可以满足您的要求:

<Style TargetType="{x:Type TabPanel}">
    <Setter Property="Background" Value="Black"/>
</Style>

If ShadeOfGrey answer does not work, you should use Grid instead of TabPanel :如果 ShadeOfGrey 答案不起作用,您应该使用Grid而不是TabPanel

<TabControl.Resources>
    <Style TargetType="{x:Type Grid}">
        <Setter Property="Background" Value="WhiteSmoke"/>
    </Style>
</TabControl.Resources>

You should set the style for the TabPanel... Basically we arrange the Tabs in the TabPanel in the TabControl.您应该为 TabPanel 设置样式...基本上我们在 TabControl 中安排 TabPanel 中的 Tabs。

The below code will help you..下面的代码会帮助你..

<TabControl Background="#123" TabStripPlacement="Left" HorizontalAlignment="Stretch" BorderBrush="#41020202">
            <TabControl.BitmapEffect>
                <DropShadowBitmapEffect Color="Black" Direction="270"/>
            </TabControl.BitmapEffect>
            <TabControl.Resources>
                <Style TargetType="{x:Type TabPanel}">
                    <Setter Property="Background" Value="Yellow"/>
                </Style>
                <Style TargetType="{x:Type TabItem}">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Padding" Value="0" />
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Border x:Name="grid" Background="Red">
                                    <ContentPresenter>
                                        <ContentPresenter.Content>
                                            <TextBlock Margin="4" FontSize="15" Text="{TemplateBinding Content}"/>
                                        </ContentPresenter.Content>
                                        <ContentPresenter.LayoutTransform>
                                            <RotateTransform Angle="270" />
                                        </ContentPresenter.LayoutTransform>
                                    </ContentPresenter>
                                </Border>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TabItem}},Path=IsSelected}" Value="True">
                                        <Setter TargetName="grid" Property="Background" Value="Green"/>
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>
            <TabItem Header="Tab Item 1" />
            <TabItem Header="Tab Item 2" />
            <TabItem Header="Tab Item 3" />
            <TabItem Header="Tab Item 4" />
        </TabControl>

The above solution didn't work for me but I had my Tab Control in a User Control and not a window.上述解决方案对我不起作用,但我的选项卡控件位于用户控件而不是窗口中。 Setting User Control background colour instead fixed the issue;设置用户控制背景颜色改为解决问题; maybe this will be helpful for others with the same problem if the up-voted solution does not work.如果投票赞成的解决方案不起作用,这可能会对其他有同样问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM