简体   繁体   English

更改多个Tab控件的Tabitem颜色

[英]Change color of tabitem for multiple tabcontrols

I use two tabcontrols in my project. 我在项目中使用了两个tabcontrols。 How can I change color for the tabitem which tabcontrol is currently active/focused? 如何为当前处于活动状态/已聚焦的tabcontrol的tabitem更改颜色? Here is the xaml sample: 这是xaml示例:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TabControl x:Name="First" Grid.Column="0">
        <TabItem Header="Test1" Margin="-2,-2,-2,0.2" Width="44" >
            <Label>Text</Label>
        </TabItem>
        <TabItem Header="Test2" Margin="-2,-2,-2,0.2" Width="44" >
            <Label>Text</Label>
        </TabItem>
    </TabControl>
    <TabControl x:Name="Second" Grid.Column="1">
        <TabItem Header="Test1" Margin="-2,-2,-2,0.2" Width="44" >
            <Label>Text</Label>
        </TabItem>
        <TabItem Header="Test2" Margin="-2,-2,-2,0.2" Width="44" >
            <Label>Text</Label>
        </TabItem>
    </TabControl>
</Grid>

It should looks like 它看起来像 在此处输入图片说明

EDIT: Changed your labels to Textboxes to demonstrate the needed behavior and changed the trigger property to IsKeyboardFocusWithin. 编辑:将标签更改为文本框以演示所需的行为,并将触发器属性更改为IsKeyboardFocusWithin。

Here you go: 干得好:

<Window x:Class="WpfTestApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Border>
                            <Grid>
                                <Grid>
                                    <Border x:Name="border" 
                                            CornerRadius="3,3,0,0"
                                            Background="WhiteSmoke"/>
                                </Grid>
                                    <ContentPresenter ContentSource="Header"
                                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"
                                     Value="True">
                                <Setter TargetName="border"
                                        Property="Background"
                                        Value="LightGray" />
                            </Trigger>
                            <Trigger Property="IsKeyboardFocusWithin"
                                     Value="True">
                                <Setter TargetName="border"
                                        Property="Background"
                                        Value="Yellow" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <Grid Name="MainGrid" Loaded="MainGrid_Loaded">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TabControl x:Name="First" Grid.Column="0">
            <TabItem Header="Test1" Margin="-2,-2,-2,0.2" Width="44" >
                <TextBox>Replace this text</TextBox>
            </TabItem>
            <TabItem Header="Test2" Margin="-2,-2,-2,0.2" Width="44" >
                <TextBox>Replace this text</TextBox>
            </TabItem>
        </TabControl>
        <TabControl x:Name="Second" Grid.Column="1">
            <TabItem Header="Test1" Margin="-2,-2,-2,0.2" Width="44" >
                <TextBox>Replace this text</TextBox>
            </TabItem>
            <TabItem Header="Test2" Margin="-2,-2,-2,0.2" Width="44" >
                <TextBox>Replace this text</TextBox>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

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

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