How can I unselect item in ListView?

后端 未结 8 1165
故里飘歌
故里飘歌 2021-01-08 01:19

I have a ListView with a couple of items in it. When the ListView looses focus, the last selected ListViewItem is still \"selected\" with a gray background.
I would like

8条回答
  •  遥遥无期
    2021-01-08 01:41

    Suppose you are accessing the ListView from a parent form/control.

    You can add this piece of code in the form's/control's constructor/load event:

    this.myListView.LostFocus += (s, e) => this.myListView.SelectedIndices.Clear();
    

    Ok, so in your case, you would replace that delegate with:

    if (this.myListView.SelectedIndices.Count > 0)
        for (int i = 0; i < this.myListView.SelectedIndices.Count; i++)
        {
            this.myListView.Items[this.myListView.SelectedIndices[i]].Selected = false;
        }
    

    You can give the code a nicer form, btw.

提交回复
热议问题