ListView unselect item

Hi guys, is there a way to unselect a listview item without reassigning the data list to it?

At the beginning the ListView is empty and I have a list (data list) containing the data (items) to display in the ListView that is also empty at the beginning.
I insert a data in the data list pressing a button and I have to show all the data in the ListView.

To update the ListView, every time I add an item to the list, I reassign the list to the listview (assigning the data list to .Elements).

The ListView is used to display all the list items and to be able to select one of them (selection color blue).
Then I can edit the selected element (opening a Notifier ShowText) and save it pressing a button.

When I press the button I want to unselect the item and to see it in black, not in blue.
Actually to produce this result I write 0 in the ListView .SelectionIndex and then I reassign the data list to the ListView.

I want to know if there is a simpler way to implement this behaviour.


Just reassigning the list to the listview will clear the selection....

So there is no way without reassigning the list...

I'm not sure if anything has changed between the behaviour between Oct 2021 and now, but this is what I see in Oct 2023.

The documentation for SelectedIndex implies that setting it to 0 should clear the selection. What happens for me is:

  • If the list is built from a list of strings, then setting SelectedIndex to 0 works, and removes the selection.
  • If the list is built from a list of dictionaries (e.g. by using ListView.CreateElement to create the elements) then then setting SelectedIndex to 0 throws an exception.

I've found that just calling ListView.Refresh removes the selection, but this has the side-effect of scrolling the list back to the top.

Thus my work around for the moment is to use PositionPx and JumpToPx from the ListViewPlus extension to put the list back in the same place...

Maybe this will help someone in the future who is looking for a fix for the same problem.

Cheers, TIm.

1 Like