Click here to Skip to main content
14,266,113 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

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 :((

Thanks
Posted
Updated 5-Jul-19 1:05am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,
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


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

                        </Condition>
                        <Condition Property="Selector.IsSelectionActive" Value="False">
                        </Condition>
                    </MultiTrigger.Conditions>
                  -------set the template here too---
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

I found another soluthen in the MSDN forum.
The gray brush resource of the ListView is overwritten.
<listview>
  <listview.resources>
    <solidcolorbrush x:key="{x:Static SystemColors.ControlBrushKey}" xmlns:x="#unknown">
        Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}"/>
  </solidcolorbrush></listview.resources>
</listview>
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

MyListBox.Resources[SystemColors.InactiveSelectionHighlightBrushKey] = SystemColors.HighlightBrush;
MyListBox.Resources[SystemColors.InactiveSelectionHighlightTextBrushKey] = SystemColors.HighlightTextBrush;

It works for me!
   
Comments
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.

https://www.codeproject.com/Questions/752101/Why-is-Setting-InactiveSelectionHighlightBrushKey

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100