Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question ListView

Discussion in 'UI Toolkit' started by qinghua_1223, Sep 14, 2022.

  1. qinghua_1223

    qinghua_1223

    Joined:
    Aug 19, 2022
    Posts:
    2
    as seeing in the picture, my code is below:
    const int itemNum = 5;
    var items = new List<string>(itemNum);
    for(int i = 0; i < itemNum; ++i)
    {
    items.Add(i.ToString());
    }
    var uiDoc = GetComponent<UnityEngine.UIElements.UIDocument>();
    var rootElement = uiDoc.rootVisualElement;
    var listView = rootElement.Q("List_server") as UnityEngine.UIElements.ListView;
    listView.makeItem = () => new Label();
    Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items;

    listView.bindItem = bindItem;
    listView.itemsSource = items;

    when scolling listview that bug thrown ArgumentOutOfRangeException: Index was out of range
     

    Attached Files:

  2. ncerone_unity

    ncerone_unity

    Unity Technologies

    Joined:
    Jan 13, 2022
    Posts:
    33
    Hi,

    Using your code I had a error on this line:
    Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items;

    Changing it to:
    Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items;

    Which makes it compile, I'm not able to reproduce your ArgumentOutOfRangeException issue.
     
  3. qinghua_1223

    qinghua_1223

    Joined:
    Aug 19, 2022
    Posts:
    2

    sorry codes is, i donot known why:
    Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items;


    but also had that bug.And function bindItem triggled, when scrolling, items were all visible in listview, i thought that function should not triggled,i donot known why that happened.