如何更改 TabControl 中选定选项卡的颜色? [英] How to change the color of the selected tab in the TabControl?

查看:61
本文介绍了如何更改 TabControl 中选定选项卡的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing a TabControl for 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).

我该怎么做?

这段代码不起作用:

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

注意:我还尝试了触发器属性的 IsMouseCaptured 事件.还是不行.

Note: I also tried IsMouseCaptured event for the trigger property. Still does not work.

推荐答案

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

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:

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

这篇关于如何更改 TabControl 中选定选项卡的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆