MultiColumnListView bindCell and cell-template

Hello,

I’m starting a new project with UI Toolkit. I’m trying to create a MultiColumnListView, and I have a problem when using bindCell in my C# script and cell-template in the UXML file (same problem with cellTemplate in the C# script).

Here is the code :

Main File :

<engine:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:engine="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
    <Style src="project://database/Assets/UI/Styles/Doc.uss" />
    <engine:MultiColumnListView allow-add="false" allow-remove="false" reorder-mode="Animated" show-bound-collection-size="false" show-border="false" name="MCListView" virtualization-method="DynamicHeight" selection-type="Single">
        <engine:Columns reorderable="false" resize-preview="false" resizable="false">
            <engine:Column name="Col1" title="Col1" stretchable="true" optional="false" sortable="false" cell-template="project://database/Assets/UI/Documents/Template/Test.uxml" resizable="false" />
        </engine:Columns>
    </engine:MultiColumnListView>
</engine:UXML>

Template File:

<engine:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:engine="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
    <Unity.AppUI.UI.TextField name="Cell" />
</engine:UXML>

C# Script:

// Start is called once before the first execution of Update after the MonoBehaviour is created
private void Start()
{
    // Populate my item list ...

    UIDocument doc = GetComponent<UIDocument>();

    MultiColumnListView multiColumnListView = (MultiColumnListView)doc.rootVisualElement.Query("MCListView");

    multiColumnListView.columns["Col1"].bindCell = (ve, i) =>
    {
        ve.Q<Unity.AppUI.UI.TextField>("Cell").value = list[i];
    };

    multiColumnListView.itemsSource = list;
}

My problem is :

When I start the game with this document, I get this message (Sometimes it work, but most of the time not):

NullReferenceException: Object reference not set to an instance of an object
MainBehaviour+<>c__DisplayClass0_0.<Start>b__1 (UnityEngine.UIElements.VisualElement ve, System.Int32 i) (at Assets/Scripts/UI/Test.cs:24)

I tried to debug it, and in the “bindCell” function the variable “ve” is a “Label” instead to be the content of my cell-template, I think the document is not yet loaded, so the query fail.

So my question is: Am I doing it wrong? Do I need an event to set the “bindCell” function? Can you help me?

Best Regards.

Hello,

I found a solution, adding that line in the c# script :

        multiColumnListView.columns["Col1"].makeCell = () =>
        {
            return multiColumnListView.columns["Col1"].cellTemplate.Instantiate();
        };

It’s not very elegant, and it should be done automatically I think.

Do you have a better Idea?

Best Regards.

Hello,

I think I’ve found the problem and maybe a bug.

First, I copied my project to several PCs and that’s where the problem occurred.

To reproduce this problem:

  • Step 1: I fill the “cell-template” with the UI Builder - everything works.

  • Step 2: I rebuild the Library, deleting the “Library” folder from the project.

  • Step 3: Nothing works, I get the exception of the first message.

I think something similar happens when I move the project to another PC.

However, when I use the following code and explicitly specify the template, it works all the time:

<engine:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:engine="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
    <Style src="project://database/Assets/UI/Styles/Doc.uss" />

    <engine:Template name="test" src="project://database/Assets/UI/Documents/Template/Test.uxml"/>

    <engine:MultiColumnListView allow-add="false" allow-remove="false" reorder-mode="Animated" show-bound-collection-size="false" show-border="false" name="MCListView" virtualization-method="DynamicHeight" selection-type="Single">
        <engine:Columns reorderable="false" resize-preview="false" resizable="false">
            <engine:Column name="Col1" title="Col1" stretchable="true" optional="false" sortable="false" cell-template="test" resizable="false" />
        </engine:Columns>
    </engine:MultiColumnListView>
</engine:UXML>

Do you think I’m right or am I doing something wrong?

Best Regards.