WPF - TabItem 选择或悬停在 TabItem 上时,背景颜色会发生变化 [英] WPF - TabItem Background color changes when tabitem selected or hover over

查看:24
本文介绍了WPF - TabItem 选择或悬停在 TabItem 上时,背景颜色会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 xaml 中 tabitem 的背景颜色设置为红色,但是当我运行它并将鼠标悬停在它上面或选择它时,它会变回默认的灰色外观.它仅在选择其他 tabitem 时正确显示.我如何一直保持红色.谢谢!

I set the Background color of the tabitem in the xaml to RED, but when i run it and hover over it or select it it changes back the the default greyish looking. It only shows correctly when the other tabitem is selected. How do i keep it RED all the time. Thanks!

推荐答案

这是 TabItem ControlTemplate 的示例

将其复制到您的资源中,并在您需要的任何地方设置红色作为背景.

Copy it to your resources and set wherever you need Red color as Background.

样品

<Window x:Class="TestCustomTab.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <SolidColorBrush x:Key="RedBrush" Color="Red"/>       

        <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />

        <SolidColorBrush x:Key="GreenBrush" Color="Green" />

        <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />       

        <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />        

        <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border 
                                Name="Border"
                                Margin="0,0,-4,0" 
                                Background="{StaticResource RedBrush}"
                                BorderBrush="{StaticResource  SolidBorderBrush}" 
                                BorderThickness="1,1,1,1" 
                                CornerRadius="2,12,0,0" >
                                <ContentPresenter x:Name="ContentSite"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center"
                                    ContentSource="Header"
                                    Margin="12,2,12,2"
                                    RecognizesAccessKey="True"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Panel.ZIndex" Value="100" />
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource GreenBrush}" />
                                <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
                                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>        
    </Window.Resources>
    <Grid>
        <TabControl>
            <TabItem Header="MyTabItem" />
            <TabItem Header="MyTabItem"  />
        </TabControl>
    </Grid>
</Window>

创建测试 WPF 项目并粘贴示例代码而不是 Window1.xaml 代码.

Create test WPF project and paste sample code instead of Window1.xaml code.

这篇关于WPF - TabItem 选择或悬停在 TabItem 上时,背景颜色会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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