How can I get a TreeNode in TreeView

Hello, My code is like:

var tree= root.Q<TreeView>("tree");
tree.makeItem = () =>
{
    var node = new TreeNode();
    return node;
};

//Add EventRootNode
var roots = new List<TreeViewItemData<Data>>();
var eventRoot = new TreeViewItemData<Data>(1, new Data());
roots.Add(eventRoot);

tree .SetRootItems(roots);

And I try to get EventRootNode later like this:

var index = treeEcas.viewController.GetIndexForId(1);
var eventRootNode = tree .ElementAt(index) as TreeNode;
//Do something with eventRootNode 

It goes wrong.

I need some help please. Thank you.

Hi @Link3,

You can’t retrieve the TreeNode for a given index or id because the content is virtualized. Instead, what you need to do is implement the bindItem callback and move your logic inside of that callback. That way, whenever a TreeNode is bound, the callback will be called.

Hope this helps!