Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Clear ListView Selection

Discussion in 'UI Toolkit' started by opsive, Aug 29, 2019.

  1. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    4,834
    What is the proper way to clear the selected ListView element? I've tried ListView.Refresh but that doesn't deselect the element, and when I try to set ListView.selectedIndex to -1 it throws an error saying the value can't be negative.

    Thank you!
     
  2. XGT08

    XGT08

    Joined:
    Aug 1, 2013
    Posts:
    1,456
    I am encountering a similar difficulty. There seems to be no way of to modify the list view selection. I am implementing a ListView in which the user can delete the selected items. This works by removing the items from the source list, but it seems that after I refresh the list view, items which were not selected before, become selected. It is as if the selection information has been mapped to other items.

    Also, it would be great if we could get access to all selected indices. I am using a list view with Multiselect caps. It would make sense that I should be able to retrieve a list with all selected item indices. This is actually useful in situations where you have to operate with the selected items because you don't have to store additional information yourself.
     
  3. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    951
    There is a
    ClearSelection()
    method but it's marked protected. It's something that needs to be fixed. In the mean time you can inherit from ListView and expose it or copy our ListView implementation and marking it public (see below).

    See my reply in your original thread:
    https://forum.unity.com/threads/lis...script-shift-multiselect.738353/#post-4927310
     
    opsive likes this.
  4. GesunderApfel

    GesunderApfel

    Joined:
    Apr 4, 2015
    Posts:
    7
    How does the implementation work? I tried to copy the ListView script but it just throws errors because many functions and properties arent available. I dont think your solution suggests I import all other dependent scripts too?
     
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I haven't really looked at this, but simply extending it would be something like:
    Code (csharp):
    1. public new void ClearSelection()
    2. {
    3.     base.ClearSelection();
    4. }
    Copying over all of the code probably won't work easily, without a lot of effort re-implementing internal methods being called out to. If you're going to go that far, just use reflection to call the protected ClearSelection method externally- it'd be less work.

    That said, it looks like it's public now anyways though.
     
    Last edited: May 4, 2020
    GesunderApfel likes this.
unityunity