prevent listview to lose selected item

后端 未结 6 1817
失恋的感觉
失恋的感觉 2021-01-18 03:43

I\'m currently working on a listview in winform c# and everytime I click on an empty space on the listview, the selected item is lost.

6条回答
  •  执笔经年
    2021-01-18 04:01

    I thought there was a property that prevented this from happening, but now I can't find it.

    You could try this:

    private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListView listView = sender as ListView;
        if (listView.SelectedItems.Count == 0)
            foreach (object item in e.RemovedItems)
                listView.SelectedItems.Add(item);
    }
    

提交回复
热议问题