简体   繁体   English

WPF ListBoxItem:按样式覆盖 SystemColors 无效

[英]WPF ListBoxItem: Overwrite SystemColors by Style has no effect

I'd like to overwrite SystemColors of a ListBoxItem .我想覆盖ListBoxItem的 SystemColors 。 My target is to change the background of a selected but not focused item.我的目标是更改选定但未聚焦项目的背景。 By default it is grayed out when focus is lost eg.默认情况下,当失去焦点时它是灰色的,例如。 by showing the context menu.通过显示上下文菜单。 I do not want fancy complex style-sugar-code, just something very simple.我不想要花哨的复杂样式糖代码,只是一些非常简单的东西。 I found several threads on the net about overwrite the SystemColors but non of them worked.我在网上发现了几个关于覆盖 SystemColors 的线程,但没有一个有效。

The following copy-paste-code does not work and the brushes will be ignored and it has no effect.以下复制粘贴代码不起作用,画笔将被忽略,没有效果。

<Window x:Class="WpfAppListBoxItemBrushes.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d"
    Title="MainWindow" Height="200" Width="400">
<ListBox>
    <ListBox.ItemsSource>
        <x:Array Type="sys:String">
            <sys:String>1st item</sys:String>
            <sys:String>2nd item</sys:String>
        </x:Array>
    </ListBox.ItemsSource>
    
    <ListBox.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Test"/>
        </ContextMenu>
    </ListBox.ContextMenu>

    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.ActiveBorderBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ActiveBorderColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ActiveCaptionBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ActiveCaptionColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ActiveCaptionTextBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ActiveCaptionTextColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkDarkBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkDarkColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlLightBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlLightColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlLightLightBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlLightLightColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveBorderBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveBorderColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveCaptionBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveCaptionColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveCaptionTextBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveCaptionTextColorKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Red" />
            </Style.Resources>
        </Style>
    </ListBox.Resources>
</ListBox>

The style definition has no effect on the ListBoxItem at all.样式定义对 ListBoxItem 完全没有影响。 What's wrong with this code?这段代码有什么问题?

在此处输入图像描述

Try to move colors to Resources of ListBox .尝试将 colors 移动到ListBoxResources中。 It should alters system colors only for this ListBox and its children ( ListboxItems ).它应该仅针对此ListBox及其子项 ( ListboxItems ) 更改系统 colors。

<ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ActiveBorderBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ActiveBorderColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ActiveCaptionBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ActiveCaptionColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ActiveCaptionTextBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ActiveCaptionTextColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkDarkBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkDarkColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlLightBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlLightColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlLightLightBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlLightLightColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveBorderBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveBorderColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveCaptionBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveCaptionColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveCaptionTextBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveCaptionTextColorKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Red" />
    </ListBox.Resources>

I am afraid this is not possible because the brushes are hardcoded in the default template of the ListBoxItem .恐怕这是不可能的,因为画笔在ListBoxItem的默认模板中是硬编码的。

So you will have to copy the entire template into your XAML markup and then modify it as per your requirements.因此,您必须将整个模板复制到您的 XAML 标记中,然后根据您的要求进行修改。

You cannot "override" the brushes without using a custom template.如果不使用自定义模板,您将无法“覆盖”画笔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM