WPF How to change the text color of a selected list when the list box loses focus

I was looking for how to change the text color of a selected item in a list that has lost focus.

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Orange"/> 

These three tags do most of the work, but there is a black background on my list, and when the control loses focus, the font becomes black.

I found this list from another SystemColor post. Keys , which gives a ton of possible options from this list, and everything that seems remotely intuitive did not work. Does anyone know the key I need to change?

+5
styles wpf listbox selecteditem
Jun 24 '10 at 16:10
source share
2 answers

I put this in the resource dictionary for the element containing the list:

  <Style TargetType="ListBoxItem"> <Style.Resources> <!--SelectedItem with focus--> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/> <!--SelectedItem without focus--> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="White"/> </Style.Resources> </Style> 

Note that in .Net 4.5 you must ask for the "old" behavior by setting

  FrameworkCompatibilityPreferences. AreInactiveSelectionHighlightBrushKeysSupported = false; 

at the beginning of your program before creating any windows.

+8
Aug 02 2018-12-12T00:
source share

use the following code and just change the colors e.g. using Colors.Black

 listBox.Resources.Add(SystemColors.ControlBrushKey, new SolidColorBrush(Color.FromArgb(0xFF, 0x7F, 0xDB, 0x14))); listBox.Resources.Add(SystemColors.ControlTextBrushKey, Brushes.White); 

Good luck.

+1
Jun 26 2018-12-12T00:
source share



All Articles