简体   繁体   English

如何更改TabControl中所选选项卡的颜色?

[英]How to change the color of the selected tab in the TabControl?

I am implementing a TabControl for a dialog box in WPF. 我正在为WPF中的对话框实现一个TabControl The color of the selected tab (mouse-down) is white by default. 默认情况下,所选选项卡(鼠标向下)的颜色为白色。 I want to change the color of that selected tab to the color of hover (when I hover over a tab, the color of the tab changes to an Office-blue-gradient, which is what I want the color of the selected tab to be on mouse-click). 我想将所选标签的颜色更改为悬停颜色(当我将鼠标悬停在标签上时,标签的颜色变为Office蓝色渐变,这就是我想要所选标签的颜色鼠标单击)。

How can I do that? 我怎样才能做到这一点?

This piece of code does not work: 这段代码不起作用:

<Style x:Key="StyleTabControl" TargetType="{x:Type TabItem}">
    <Setter Property="Background" Value="#FFFDFDFD"/>
    <Style.Triggers>
        <Trigger Property="IsSelected "  Value="True">
            <Setter Property="Background" Value="SlateGray"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Note: I also tried IsMouseCaptured event for the trigger property. 注意:我还尝试了触发器属性的IsMouseCaptured事件。 Still does not work. 仍然无法正常工作。

Alright...after hours of trying, I have realized that the TabItem selection behaviour is defined at the Template level. 好吧......经过几个小时的尝试,我意识到TabItem选择行为是在模板级别定义的。 So, if I wana change the backgrnd color, I do this: 所以,如果我改变背景颜色,我会这样做:

<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="IsMouseOver"
                                     Value="True">
                                <Setter TargetName="border"
                                        Property="Background"
                                        Value="LightGray" />
                            </Trigger>
                            <Trigger Property="IsSelected"
                                     Value="True">
                                <Setter TargetName="border"
                                        Property="Background"
                                        Value="LightGray" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

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

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