Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF SelectedItem color disappears when the list loses the focus

I have a WPF Window which basically has a ListBox which displays a list of items.

I then have a ListView basically displaying the details of the SelectedItem in the ListBox.

The thing is, when the focus is not on the ListBox anymore, the highlighting color disappears and I cannot see which master item was selected anymore.

Do you know how I can solve this? (ie. making sure that the item stays highlighted)

like image 445
SRKX Avatar asked Jul 08 '11 12:07

SRKX


2 Answers

The quickest way is to use a style on the ListBoxItem to override the default System colors:

<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.ControlBrushKey}" 
         Color="LightBlue" Opacity=".4"/>
   </Style.Resources>
</Style>

This is kind of a shortcut to the slightly more complex (but easier to control) method of defining the ItemTemplate for the list items. There are plenty of examples of that online, so I won't put that here.

like image 111
Wonko the Sane Avatar answered Sep 27 '22 21:09

Wonko the Sane


Check out this MSDN blog post

like image 36
Phil Gan Avatar answered Sep 27 '22 23:09

Phil Gan