gpt4 book ai didi

WPF - 选择 Tabitem 或将鼠标悬停在其上时 TabItem 背景颜色会发生变化

转载 作者:行者123 更新时间:2023-12-01 23:54:40 25 4
gpt4 key购买 nike

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

最佳答案

Here is example of TabItem ControlTemplate

将其复制到您的资源中,并在需要红色作为背景的位置设置。

示例

<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 代码。

关于WPF - 选择 Tabitem 或将鼠标悬停在其上时 TabItem 背景颜色会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3265140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com