I'd like to overwrite SystemColors of a ListBoxItem. 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.
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. What's wrong with this code?
CodePudding user response:
Try to move colors to Resources
of ListBox
. It should alters system colors only for this ListBox
and its children (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>
CodePudding user response:
I am afraid this is not possible because the brushes are hardcoded in the default template of the ListBoxItem
.
So you will have to copy the entire template into your XAML markup and then modify it as per your requirements.
You cannot "override" the brushes without using a custom template.