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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Having trouble with ListView's itemMake/itemBind parameters

Discussion in 'UI Toolkit' started by niel-archer, Sep 26, 2021.

  1. niel-archer

    niel-archer

    Joined:
    Nov 29, 2016
    Posts:
    17
    I'm following this example for ListView https://docs.unity3d.com/Packages/com.unity.ui@1.0/api/UnityEngine.UIElements.ListView.html#UnityEngine_UIElements_ListView and I need to use a list entry more complicated than a simple Label.

    I've created a class (called ListEntry)., that extends VisualElement, to hold four labels ListEntry will contain a method to do the binding of another object (ListItem) to the ListEntry.

    How would I create the itemMake and ItemBind parameters to do that, or am I fundamentally misunderstanding this?
     
    Last edited: Sep 27, 2021
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    442
    Hi @niel-archer !

    There's a simple example on how to set up a ListView in the sample window. (Window/UI Toolkit/Samples)

    Would something like this work for your use case ?
    Func<VisualElement> makeItem = () => new ListEntry();
    Action<VisualElement, int> bindItem = (e, i) => (e as ListEntry).item = items[i];
     
  3. niel-archer

    niel-archer

    Joined:
    Nov 29, 2016
    Posts:
    17
    I saw that example, and as stated in OP, I have a more complicated entry with more than one field. I have found a way to do it using similar method to that example, but I would really like to put the code into the class for the new ListEntry so that it is all together and I don't have to crawl through hundreds or thousands of lines of code to find it if/when it needs to be updated.
     
  4. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    442
    I think I understand. You could add a BindItem method in your ListEntry class that holds the binging logic. Then call this method in the bindItem callback. The result would look something like this.
    Code (CSharp):
    1. Action<VisualElement, int> bindItem = (e, i) => (e as ListEntry).BindItem(items[i]);
    Does it answer your question?
     
  5. niel-archer

    niel-archer

    Joined:
    Nov 29, 2016
    Posts:
    17
    Many thanks. That looks like what I am after. I will give it a try as soon as I can find time.
     
    HugoBD-Unity likes this.
  6. niel-archer

    niel-archer

    Joined:
    Nov 29, 2016
    Posts:
    17
    Thanks for your help, that was exactly what I needed.