wpf-controls - WPF控件当选定或者悬停时控件的WPF背景色更改

  显示原文与译文双语对照的内容
59 0

我将xaml中的nmake背景色设置为红色,但当运行它并悬停在它上方或者者选择它的默认灰色。选择其他tabitem时,它只显示正确。我怎么让它一直保持红色。谢谢!

时间:原作者:0个回答

122 5

以下是of的例子

将它复制到你的资源并设置为你需要红色作为背景的地方。

样本

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

原作者:
...