当鼠标悬停在TabItem上时,我不想要两个蓝调

时间:2018-02-25 17:59:46

标签: c# wpf vb.net xaml

这是XAML代码。

当鼠标悬停在TabItem上时,我不想要两个蓝调。

当鼠标悬停在TabItem上时,我不想要两个蓝调。

当鼠标悬停在TabItem上时,我不想要两个蓝调。

当鼠标悬停在TabItem上时,我不想要两个蓝调。

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="200" Width="400">

<Window.Resources>
    <LinearGradientBrush x:Key="TabItemHotBackground" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="Blue" Offset="0.15"/>
        <GradientStop Color="Blue" Offset=".5"/>
        <GradientStop Color="Blue" Offset=".5"/>
        <GradientStop Color="Blue" Offset="1"/>
    </LinearGradientBrush>

    <Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid SnapsToDevicePixels="true">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                            <ContentPresenter x:Name="Content" ContentSource="Header" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" TargetName="Bd" Value="{StaticResource TabItemHotBackground}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>


<Grid>
    <TabControl Background="Pink">
        <TabItem Background="Yellow" Style="{DynamicResource TabItemStyle1}">
            <TabItem.Header>
                <StackPanel>
                    <TextBlock Text="Customer" HorizontalAlignment="Center"/>
                    <TextBlock Text="Name" HorizontalAlignment="Center"/>
                </StackPanel>
            </TabItem.Header>
        </TabItem>
        <TabItem Background="Yellow" Style="{DynamicResource TabItemStyle1}">
            <TabItem.Header>
                <StackPanel>
                    <TextBlock Text="Customer" HorizontalAlignment="Center"/>
                    <TextBlock Text="Address" HorizontalAlignment="Center"/>
                </StackPanel>
            </TabItem.Header>
        </TabItem>
    </TabControl>
</Grid>
</Window>

问题:

enter image description here

所以,当鼠标悬停在TabItem上时,我不想要两个蓝调。

1 个答案:

答案 0 :(得分:2)

更改它的最简单方法是编辑TabItem模板,右键点击来自vs designer EditTemplate&gt;的TabItem编辑副本,然后使用密钥LinearGradientBrush查找TabItem.MouseOver.Background,使用您的颜色将其更改为SolidColorBrush(并保持相同的密钥):

<SolidColorBrush x:Key="TabItem.MouseOver.Background" Color="Blue"/>

或更新GradiantStop点以使用相同的颜色:

<LinearGradientBrush x:Key="TabItem.MouseOver.Background" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="Blue" Offset="0.0"/>
        <GradientStop Color="Blue" Offset="1.0"/>
</LinearGradientBrush>