WPF ListBoxItem:按样式覆盖 SystemColors 无效
WPF ListBoxItem: Overwrite SystemColors by Style has no effect
我想覆盖 ListBoxItem 的 SystemColors。我的目标是更改 选中但未聚焦的 项目的背景。默认情况下,当失去焦点时它是灰色的,例如。通过显示上下文菜单。我不想要花哨的复杂样式糖代码,只是一些非常简单的东西。我在网上发现了几个关于覆盖 SystemColors 的线程,但没有一个有效。
下面的copy-paste-code不起作用,刷机会被忽略,没有效果
<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>
样式定义对ListBoxItem 没有任何影响。这段代码有什么问题?
尝试将颜色移至 ListBox
的 Resources
。它应该仅为 ListBox
及其子项 (ListboxItems
) 更改系统颜色。
<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>
恐怕这是不可能的,因为画笔是硬编码在 ListBoxItem
的默认模板中的。
因此您必须将整个模板复制到您的 XAML 标记中,然后根据您的要求进行修改。
您不能在不使用自定义模板的情况下“覆盖”画笔。
我想覆盖 ListBoxItem 的 SystemColors。我的目标是更改 选中但未聚焦的 项目的背景。默认情况下,当失去焦点时它是灰色的,例如。通过显示上下文菜单。我不想要花哨的复杂样式糖代码,只是一些非常简单的东西。我在网上发现了几个关于覆盖 SystemColors 的线程,但没有一个有效。
下面的copy-paste-code不起作用,刷机会被忽略,没有效果
<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>
样式定义对ListBoxItem 没有任何影响。这段代码有什么问题?
尝试将颜色移至 ListBox
的 Resources
。它应该仅为 ListBox
及其子项 (ListboxItems
) 更改系统颜色。
<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>
恐怕这是不可能的,因为画笔是硬编码在 ListBoxItem
的默认模板中的。
因此您必须将整个模板复制到您的 XAML 标记中,然后根据您的要求进行修改。
您不能在不使用自定义模板的情况下“覆盖”画笔。