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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18470918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:30:49  来源:igfitidea点击:

How to change the color of the selected tab in the TabControl?

c#wpfxamltabstabcontrol

提问by aromore

I am implementing a TabControlfor a dialog box in WPF. 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).

我正在TabControlWPF 中实现一个对话框。所选选项卡(鼠标按下)的颜色默认为白色。我想将所选选项卡的颜色更改为悬停的颜色(当我将鼠标悬停在选项卡上时,选项卡的颜色更改为 Office-blue-gradient,这就是我希望所选选项卡的颜色单击鼠标)。

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. Still does not work.

注意:我还尝试了触发器属性的 IsMouseCaptured 事件。仍然不起作用。

回答by aromore

Alright...after hours of trying, I have realized that the TabItem selection behaviour is defined at the Template level. So, if I wana change the backgrnd color, I do this:

好吧...经过数小时的尝试,我意识到 TabItem 选择行为是在模板级别定义的。所以,如果我想改变背景颜色,我会这样做:

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