UI Toolkit ListView UXML binding

Hi,

I tried to follow this documentation to achieve runtime binding for list views. Basically it worked, then I tried to define the binding in UXML directly as stated in the documentation:

<ui:ListView>
    <Bindings>
        <ui:DataBinding property="itemsSource" data-source-path="items" />
    </Bindings>
</ui:ListView>

But by doing this, I get the following 2 errors:

Element 'UnityEngine.UIElements.Bindings' is missing a UxmlElementAttribute and has no registered factory method. Please ensure that you have the correct namespace imported.
InvalidOperationException: You can't add directly to this VisualElement. Use hierarchy.Add() if you know what you're doing.

What am I doing wrong?

BR

Can you post the whole uxml file, including the UXML nodes?

<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xmlns="UnityEngine.UIElements" ue="UnityEditor.UIElements" editor-extension-mode="False">
    <ui:ListView virtualization-method="DynamicHeight" reorder-mode="Animated" binding-source-selection-mode="AutoAssign" show-add-remove-footer="true" header-title="Items" reorderable="true" show-border="false" show-foldout-header="true" item-template="ListViewItem.uxml" show-bound-collection-size="false">
        <Bindings>
            <ui:DataBinding property="itemsSource" data-source-path="items" />
        </Bindings>
    </ui:ListView>
</ui:UXML>

In this case, xmlns="UnityEngine.UIElements" is the problematic part. It informs the importer that this should be the prefix of every node that doesn’t have a prefix. If you remove this part, it should work as expected.

1 Like