Selected item loses style when focus moved out in WPF ListBox
Solution 1
If you're only setting the background color, try replacing ControlBrush
for the ListBox, as per this answer.
Solution 2
The answer referenced will in some cases solve the problem, but is not ideal as it breaks when the control is disabled/readonly and it also overrides the color schemes, rather than taking advantage of them. My suggestion is to add the following in the ListBox tags:
<ListBox....>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background"
Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
What this will do is set the Highlight background color on the list box item whenever it is selected (regardless of the control state).
My answer is based on help from the answers already given to these answers, along with the following blog: http://blogs.vbcity.com/xtab/archive/2009/06/29/9344.aspx
Related videos on Youtube
Vijay
Yet another developer trying to put some content here from the past development experience. A fan of SharePoint though, it has both bad and good sides and I my self have struggeled a lot to get things done with it. I wish to say thanks to the one who introduced it to me. Currently, I am working with Proteans Software Solutions Pvt. Ltd. in Bangalore as a Module Lead.
Updated on April 21, 2022Comments
-
Vijay about 2 years
What do I have?
I have a
ListBox
populated with items from an XML file. Given aDynamicResource
forStyle
property and written trigger forIsSelected
inItemContainerStyle
.What do I want to do?
I want to keep the selected Item highlighted even after focus moved out of the
ListBox
.What problem am I facing?
When I select an item the style specified in
IsSelected
trigger works. But, when I move the focus out of list box (press tab or click on some other control) the selected item loses its style. Is there any way so that I can retain the selected item style?Thanks in advance!
-
Vijay over 14 yearsOops!! it was by mistake. Sorry!
-
jpsstavares almost 14 yearsHi, I'm facing the same problem and tried the solution posted but I can't solve the problem. Can you edit your post so it contains the solution? thanks
-
BrainSlugs83 over 9 years@jpsstavares try the second solution (the more popular, but yet not accepted one) -- it works, and won't screw up other WPF UI Elements.
-
-
Vijay over 14 yearsOk. Let me try it out. Thanks!
-
BrainSlugs83 over 9 yearsThis is a bad idea -- for users with visual theming turned off, this will mess up a lot of things -- the scroll bars and buttons will change to become the current highlight color (this is blue by default -- so your inner buttons and ListView scrollbars turn blue, very bad!).