Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Using ListView with BindProperty to a SerializedProperty breaks ListView.itemsChosen event

Discussion in 'UI Toolkit' started by carl010010, Dec 4, 2022.

  1. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
    When choosing an item in a ListView that is bound to a SerializedProperty and you listen to the itemsChosen event, the object that is given back is incorrect. The index seems to be off by one and the first element in the list seems to return the size of the list??? I'm refencing a list of ScriptableObjects(Tables) inside of a ScriptableObject(Database).

    I've attached a picture showing the output of a debug log I call inside of the listener for itemsChosen event as I click on each element of the list. Below that the code I'm using to bind to the listview.

    upload_2022-12-4_18-41-48.png

    Code (CSharp):
    1. _tableListView = root.Query<ListView>("TablesListView");
    2.  
    3.             _tableListView.BindProperty(new SerializedObject(Database).FindProperty("Tables"));
    4.             _tableListView.makeItem += MakeLabel;
    5.             _tableListView.bindItem += BindTables(Database.Tables);
    6.             _tableListView.itemsChosen += objTable =>
    7.             {
    8.                 SerializedProperty property = objTable.FirstOrDefault() as SerializedProperty;
    9.                 Debug.Log(property.displayName + "\n\t" + property.boxedValue);
    10.             };
     
  2. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
    Looks like its an issue with ListView.showBoundCollectionSize it apparently defaults to true. But it only seems todo anything when you bind to a list view. Setting ListView.ItemSource directly doesn't care about showBoundCollectionSize, so when switching between the two it appears as a bug.
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,223