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.