为多个tabcontrols更改tabitem的颜色

时间:2015-07-29 15:40:26

标签: c# .net wpf

我在项目中使用了两个tabcontrols。如何更改tabcontrol当前活动/关注的tabitem的颜色? 这是xaml示例:

sscanf

应该是这样的 enter image description here

1 个答案:

答案 0 :(得分:0)

编辑:将标签更改为文本框以演示所需的行为,并将触发器属性更改为IsKeyboardFocusWithin。

你走了:

<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>