HighlightBrushKey 设置在 Windows 7 中不起作用

Posted

技术标签:

【中文标题】HighlightBrushKey 设置在 Windows 7 中不起作用【英文标题】:HighlightBrushKey settings not working in Windows 7 【发布时间】:2011-08-09 20:50:52 【问题描述】:

我的资源字典中定义了以下样式:

<!-- ListViewItem Styles-->
<LinearGradientBrush x:Key="x:Static SystemColors.HighlightBrushKey" StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStopCollection>
            <GradientStop Color="#F7D073" Offset="0"/>
            <GradientStop Color="#F1A62F" Offset="1"/>
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

<LinearGradientBrush x:Key="x:Static SystemColors.ControlBrushKey" StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStopCollection>
            <GradientStop Color="#F7D073" Offset="0"/>
            <GradientStop Color="#F1A62F" Offset="1"/>
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

<LinearGradientBrush x:Key="MouseOverBrush" StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStopCollection>
            <GradientStop Color="#E4F0FD" Offset="0"/>
            <GradientStop Color="#D7EAFD" Offset="1"/>
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

<Style TargetType="x:Type ListViewItem">
    <Setter Property="Foreground" Value="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type UserControl, Path=DataForeground, Converter=StaticResource ColorToBrushConverter" />
    <Setter Property="Padding" Value="1,0,1,0" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="StaticResource MouseOverBrush" />
            <Setter Property="BorderBrush" Value="#C6E1FC" />
            <Setter Property="BorderThickness" Value="1" />
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="BorderBrush" Value="#909090" />
            <Setter Property="BorderThickness" Value="1" />
        </Trigger>
    </Style.Triggers>
    <Style.Resources>
        <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="2"/>
        </Style>
    </Style.Resources>
</Style>
<!-- /ListViewItem Styles-->

当我使用 Windows XP 时,我得到的行为是我的渐变被用于高亮和选择。现在我已经切换到使用 Windows 7,似乎不再使用渐变,突出显示/选择颜色现在是 VS 外观的浅蓝色。

关于为什么会发生这种情况以及如何解决这个问题的任何建议,以便它在 Windows XP 和 Windows 7 上运行相同(我们有一个多平台环境)

谢谢。

cmets 后的完整解决方案

<LinearGradientBrush x:Key="x:Static SystemColors.HighlightBrushKey" StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStopCollection>
            <GradientStop Color="#F7D073" Offset="0"/>
            <GradientStop Color="#F1A62F" Offset="1"/>
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

<LinearGradientBrush x:Key="x:Static SystemColors.ControlBrushKey" StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStopCollection>
            <GradientStop Color="#F7D073" Offset="0"/>
            <GradientStop Color="#F1A62F" Offset="1"/>
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

<LinearGradientBrush x:Key="MouseOverBrush" StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStopCollection>
            <GradientStop Color="#E4F0FD" Offset="0"/>
            <GradientStop Color="#D7EAFD" Offset="1"/>
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

<Style TargetType="x:Type ListViewItem">
    <Setter Property="Foreground" Value="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type UserControl, Path=DataForeground, Converter=StaticResource ColorToBrushConverter" />
    <Setter Property="Padding" Value="1,0,1,0" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="StaticResource MouseOverBrush" />
            <Setter Property="BorderBrush" Value="#C6E1FC" />
            <Setter Property="BorderThickness" Value="1" />
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="DynamicResource x:Static SystemColors.HighlightBrushKey"/>
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="BorderBrush" Value="#909090" />
            <Setter Property="BorderThickness" Value="1" />
        </Trigger>
        <!-- This part of the triger is for when Windows Aero theme is turned on Win Vista/7-->
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=ListViewItem, Path=IsSelected" Value="True" />
                <Condition Binding="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=ListView, Path=IsKeyboardFocusWithin" Value="True" />
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="DynamicResource x:Static SystemColors.HighlightBrushKey"/>
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="BorderBrush" Value="#909090" />
            <Setter Property="BorderThickness" Value="1" />
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=ListViewItem, Path=IsSelected" Value="True" />
                <Condition Binding="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=ListView, Path=IsKeyboardFocusWithin" Value="False" />
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="DynamicResource x:Static SystemColors.HighlightBrushKey"/>
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="BorderBrush" Value="#909090" />
            <Setter Property="BorderThickness" Value="1" />
        </MultiDataTrigger>
    </Style.Triggers>
    <Style.Resources>
        <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="2"/>
        </Style>
    </Style.Resources>
</Style>

【问题讨论】:

【参考方案1】:

Aero 上的默认样式与 Luna 主题有些不同。在 Aero 中,默认样式中有一个类似这样的触发器:

<Trigger Property="IsSelected" Value="true">
    <Setter Property="Background" Value="StaticResource ListItemSelectedFill"/>
    <!-- ... -->
</Trigger>

在 Luna 上时,它看起来像:

<Trigger Property="IsSelected" Value="true">
    <Setter TargetName="Bd" Property="Background"
        Value="DynamicResource x:Static SystemColors.HighlightBrushKey"/>
    <!-- ... -->
</Trigger>

所以你可以看到 Aero 的默认样式,根本不使用 HighlightBrushKey。这主要是因为这些画笔基于单一的纯色。但是 Aero 主题有很多渐变色,这是旧款颜色无法表现的。

您还需要设置 Background 属性才能将其应用于 Aero,如下所示:

<Trigger Property="IsSelected" Value="true">
    <Setter Property="Background"
        Value="DynamicResource x:Static SystemColors.HighlightBrushKey"/>
    <!-- ... -->
</Trigger>

【讨论】:

这正是我所需要的。你的评论帮助我朝着正确的方向前进。我已将完整的 Aero 解决方案添加到我原来的问题中。谢谢

以上是关于HighlightBrushKey 设置在 Windows 7 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何更改 WPF 文本框的突出显示文本颜色?

win10设置里的代理怎么设置

怎样进行Win10软件高DPI设置

win10如何恢复网络设置

win10设置每天定时关机

win10 TLS安全设置未设置该怎么办