I tried to instantiate some objects inside a list view via C# script by using the below code. First I used CloneTree or instantiate method to clone the visual tree asset inside the list view. But this method creating aTemplateContainer which is preventing my events on the original visual tree.
var listView = uiDocument.rootVisualElement.Q<ListView>("WorkflowList");
var root = uiDocument.rootVisualElement;
const int itemCount = 1000;
var items = new List<string>(itemCount);
for (int i = 1; i <= itemCount; i++)
items.Add(i.ToString());
listView.Q<ScrollView>().verticalScrollerVisibility = ScrollerVisibility.Hidden;
Func<VisualElement> makeItem = () =>
{
workFlowCardTemplateTreeAsset.CloneTree(root, out int itemIndex, out int elemCount);
return root.ElementAt(itemIndex);
};
Action<VisualElement, int> bindItem = (visualElement, index) => { };
listView.makeItem = makeItem;
listView.bindItem = bindItem;
listView.itemsSource = items;
listView.selectionChanged += (selectedItems) =>
{
foreach (var selectedItem in selectedItems)
{
Debug.LogError("Item Selected... " + (selectedItem));
}
};
When you use CloneTree() without a parameter or Instantiate(), it adds a TemplateContainer that stops the line height from propagating. Use CloneTree(root) instead.
Func<VisualElement> makeItem = () =>
{
workFlowCardTemplateTreeAsset.CloneTree(root, out int itemIndex, out int elemCount);
return root.ElementAt(itemIndex);
};
Later I used CloneTree(root) method to solve that issue. But if I do like this, The Margin-Bottom property which I added to the cards to give some space between the list become zero. and It’s not showing any spaces.
I’m looking for the help to find the correct implementation of CloneTree(root) method inside the list view without creating any Template Container