Click here to Skip to main content
15,076,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

wondering how to keep a ListViewItem highlighted after moving focus to another control.

Right now I am changig the appearance of the item by setting a new ControlTemplate when the Item is selected.
Seems to me, there was a property called ListView.HideSelection in the WinForms control. Is there a way to do that in WPF as well???

Can't imagine it is that hard, though I am not getting anywhere with googling :((

Updated 5-Jul-19 1:05am

I think you are defining new Control template for the ListViewItem.Do one more think and try. Now you are changing the template on the IsSelected trigger right.You have to do the sane think on the following trigger

                        <Condition Property="Selector.IsSelected" Value="True">

                        <Condition Property="Selector.IsSelectionActive" Value="False">
                  -------set the template here too---
I found another soluthen in the MSDN forum.
The gray brush resource of the ListView is overwritten.
    <solidcolorbrush x:key="{x:Static SystemColors.ControlBrushKey}" xmlns:x="#unknown">
        Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}"/>
Force highlight in LostFocus Event and clear in GotFocus event.
Set HideSelection to True.

Private Sub lvGroup_LostFocus(sender As Object, e As EventArgs) Handles lvGroup.LostFocus
lvGroup.SelectedItems(0).BackColor = SystemColors.Highlight
lvGroup.SelectedItems(0).ForeColor = Color.White
End Sub
Private Sub lvGroup_GotFocus(sender As Object, e As EventArgs) Handles lvGroup.GotFocus
lvGroup.SelectedItems(0).BackColor = Color.White
lvGroup.SelectedItems(0).ForeColor = Color.Black
End Sub
MyListBox.Resources[SystemColors.InactiveSelectionHighlightBrushKey] = SystemColors.HighlightBrush;
MyListBox.Resources[SystemColors.InactiveSelectionHighlightTextBrushKey] = SystemColors.HighlightTextBrush;

It works for me!
Richard MacCutchan 5-Jul-19 8:00am
The question refers to a ListView, rather than a ListBox. And after 9 years the poster has most likely moved on.
Kats2512 5-Jul-19 10:07am
Just another spammer. Posted the exact same garbage to the link below at solution 2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900