How to use the itemTemplate property in ListView?

I’m trying to understand how to use the itemTemplate field on a ListView in UI Builder.

Screenshot 2024-09-27 alle 13.07.13

This is the UXML I’m trying to assign:

The TextField and ColorField’s bindingPaths are set to fields of the ScriptableObject.

The list correctly visualises and the template instantiates, but the fields are not bound and I get this in the console:

Field type UnityEngine.UIElements.TemplateContainer is not compatible with PPtr<$Persona> property "Personas.Array.data[0]"
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&) (at /Users/bokken/build/output/unity/unity/Modules/IMGUI/GUIUtility.cs:219)

Persona is a ScriptableObject type that constitutes an element in the list.

I kinda understand the issue: the item in the source collection (the SO Persona) is being bound to the root of this VisualTreeAsset, and that creates the type mismatch.

But so what’s the correct workflow to use a VisualTreeAsset assigned in the UI Builder like so?

You should not use so to bind in an item template, always use type. ListView will automatically bind those items with correct index.

Thanks, but what do you mean by this?

image
Use type instead of object, this type should be the same as your list type.

1 Like

They should grey out “Object” button then or give tooltip

Thanks Misaki! It makes sense… now.
I will try it as soon as I can.