Change the selected and unfocused list style so that it is not grayed out

I have a really simple WPF ListBox with SelectionMode set to Multiple.

<ListBox SelectionMode="Multiple" /> 

When the ListBox loses focus, it is very difficult to say what was selected because the highlight color changes from blue to light gray. What is the easiest way to change this behavior so that it stays blue?

I know this has something to do with the ListItem style, but I can't find where.

Greetings.

Similar: Inactive color selection WPF ListView

+31
wpf listbox
Mar 30 '09 at 20:08
source share
6 answers

I did something similar using the following in a combined ResourceDictionary, this may help you:

 <Style TargetType="ListBoxItem"> <Style.Resources> <!--SelectedItem with focus--> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" Opacity=".4"/> <!--SelectedItem without focus--> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }" Color="LightBlue" Opacity=".4"/> </Style.Resources> </Style> 
+47
Mar 30 '09 at 20:17
source share

This is not an answer to the question, but I found it when I was looking for a way to turn off the selection in my lists. Using a slightly modified form of Guy and Bendway technique above, it turns out that you cannot display in your list a lack of choice without replacing the control with elements or something like that. Here is the code I used:

 <Grid.Resources> <Style TargetType="ListBoxItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White" /> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="White" /> </Style.Resources> </Style> </Grid.Resources> 

I also found the following MSDN page:

MSDN: SystemColors Members (System.Windows)

Thanks for the help guys.

+10
Aug 28 '09 at 15:47
source share

A more complete solution would be to make the new brush reference HighlightColor:

 <Style TargetType="ListBox"> <Setter Property="ItemContainerStyle"> <Setter.Value> <Style TargetType="ListBoxItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static Member=SystemColors.InactiveSelectionHighlightBrushKey}" Color="{DynamicResource ResourceKey={x:Static Member=SystemColors.HighlightColorKey}}" /> </Style.Resources> </Style> </Setter.Value> </Setter> </Style> 

This ensures that it uses the same color and matches the system theme (even if the system theme changes at runtime thanks to DynamicResource).

By the way, it seems that recent versions of WPF no longer use "ControlBrush" for this, but a more suitable "InactiveSelectionHighlightBrush".

+7
04 Oct '13 at 13:22
source share

You can probably solve your problem by rewriting the template, but try this for an easy patch.

 <Style TargetType="ListViewItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" /> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue" /> </Style.Resources> </Style> 
+4
Mar 30 '09 at 20:24
source share

I also have this problem. But I solved this using IsSynchronizedWithCurrentItem = "True":

 <ListBox IsSynchronizedWithCurrentItem="True" /> 
0
Nov 21 '13 at 16:36
source share

When the selected items turn gray, this is because your control loses its logical focus. Several controls have the ability to have logical focus at the same time. A simple solution for this would be to provide a ListBox FocusScope through the FocusManager.

 <ListBox SelectionMode="Multiple" FocusManager.IsFocusScope="True"></ListBox> 
0
Oct 25 '17 at 12:58 on
source share



All Articles