鼠标悬停ComboBox项目的颜色

时间:2018-03-28 10:50:56

标签: wpf combobox

我在WPF项目中创建了combobox并应用了以下样式:

<Style TargetType="ComboBox">
        <Setter Property="Background" Value="#005578"/>
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="14"/>
    </Style>
    <Style TargetType="ComboBoxItem">
        <Setter Property="Background" Value="#005578"/>
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Height" Value="40"/>
        <Style.Triggers>
            <Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
                <Setter Property="Background" Value="#96A51E"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

当组合框关闭时#34;它将在#005578(深蓝色)中显示所选项目。 当combox被打开时#34;它应显示#005578中的所有项目和鼠标光标指向的项目#96A51E(黄色)

不幸的是,鼠标悬停颜色是深蓝色(比#05578更暗),而不是黄色,你可以在这里看到:

[ComboBox[1] 为什么会那样?

1 个答案:

答案 0 :(得分:2)

您遇到此问题的原因是您正在更改样式而不是模板。这样做会产生鼠标悬停效果。 您需要用更合适的替换它。 我使用win 10,如果我提取组合框的模板,我看到:

<ControlTemplate x:Key="ComboBoxItemControlTemplate1" TargetType="{x:Type ComboBoxItem}">
    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
        <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False"/>
                <Condition Property="IsMouseOver" Value="True"/>
                <Condition Property="IsKeyboardFocused" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="#1F26A0DA"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="#A826A0DA"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True"/>
                <Condition Property="IsMouseOver" Value="False"/>
                <Condition Property="IsKeyboardFocused" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="#3D26A0DA"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="#FF26A0DA"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True"/>
                <Condition Property="IsMouseOver" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="#2E0080FF"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="#99006CD9"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True"/>
                <Condition Property="IsMouseOver" Value="False"/>
                <Condition Property="IsKeyboardFocused" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="#3DDADADA"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="#FFDADADA"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False"/>
                <Condition Property="IsMouseOver" Value="False"/>
                <Condition Property="IsKeyboardFocused" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="BorderBrush" TargetName="Bd" Value="#FF26A0DA"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False"/>
                <Condition Property="IsMouseOver" Value="True"/>
                <Condition Property="IsKeyboardFocused" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="#5426A0DA"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="#FF26A0DA"/>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

在选择项目时,您需要考虑这是如何工作的,而不是选择等。