2010-07-16 50 views
13

我设置的TabItem的背景颜色在XAML为红色,但是当我运行它,并在它悬停或选择它,它变回了默认的灰色寻找TabItem的背景颜色的变化。只有在选中其他tabitem时它才能正确显示。我如何始终保持红色。谢谢!WPF - 当TabItem的选择或将鼠标悬停在

回答

28

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

+0

你能告诉我一个简单的例子。谢谢! – TCoder 2010-07-20 11:54:45

+0

检查与样本更新。 – 2010-07-20 12:08:45

+0

很棒!谢谢! – TCoder 2010-07-20 12:37:43