Programmatically refresh branch of RadzenTree created in Expand callback

First of all, I am quite new to Blazor and the Radzen components, but it everything was quite intuitive as the APIs just work as one would expect. However, I am now stuck with the RadzenTree.

Suppose an application like the file system example where the strucuture is dynamically loaded as the nodes for the directories are expanded. Additionally, suppose that I have added an option in the context menu of directory nodes that allows for creating child items. All of this is working as expected. However, I now want the tree to be updated as the new elements are created. The tree does update if I collapse the parent node and expand it again, so it can do it in principle, but I was unable to trigger that programmatically.

I tried so far setting the Expanded property of the parent node to false and true again. The VS analyser is complaining that I should not do that (because it is not in HTML nor data bound) and it also does not work.

I read in another post, that setting the Value would trigger a redraw, but again, I get the analyser complainig and there is no effect.

I was thinking about adding the TreeViewItem manually, but the APIs for that are not public. Changing the Data property of the parent node again generates the warning and has no effect.

Finally, I tried calling StateHasChanged on the owning page, but TBH, I don't know what this actually does and it has no effect on the tree at all.

Is there any way to programmatically force the re-evaluation of the Expand callback to update the branch below a node?

Thanks in advance,
Christoph

You can add items in a for loop since they can be declared manually:

For example:

<RadzenTree>
        @foreach(var item in Items)
        {
                <RadzenTreeItem Text="@item.Text">
                   ...
                </RadzenTreeItem>
        }
</RadzenTree>

Thanks for the quick reply. I am aware of this, but my problem is that the parent might have been created by the OnExpand callback, so I do not have markup for that. My code is very similar to the file system example:

    private void OnExpand(TreeExpandEventArgs args) {
        if (args.Value is Descriptor d) {
            args.Children.Data = this._manager.GetChildren(d.Path, this).Cast<Descriptor>().ToList();
            args.Children.Text = GetNodeLabel;
            args.Children.HasChildren = HasChildren;
            args.Children.Template = NodeTemplate;
            args.Children.Checkable = _ => false;
        }
    }

Creating the child node happens in code where I only have the parent node:

    private async Task CreateChild(RadzenTreeItem item) {
        var container = (Descriptor) item.Value;
        var title = Resources.TitleCreate;
        var parameters = new Dictionary<string, object>() {
            { nameof(CreateObjectView.Container), container.Path }
        };
        var result = await this._dialogService.OpenAsync<CreateObjectView>(title, parameters);

        if (result != null) {
            // TODO: I would need to force the update at this point.
        }
   }

The question is how I would force the update of the children of item in the line with the TODO.

You will have to switch to creating of items with @foreach - this is the only way I can think of.

That's unfortunate. I had hoped that I would be able to load the file system structure on demand only via the Expand callback.

Is there a particular reason why this is not supported in conjunction with dynamic creation of branches? I have tried the nasty hack

var mi = item.Tree.GetType().GetMethod("ExpandItem", BindingFlags.Instance | BindingFlags.NonPublic);
mi!.Invoke(item.Tree, [item]);

and the tree does exactly what I would have expected ...

You can create pull request with desired API change - if it doesn’t break anything it will be merged in our codebase.

O componente RadzenTree depende diretamente da atualização da fonte de dados (Data) e da chamada de StateHasChanged para refletir as alterações. Se o RadzenTree não está refletindo a mudança após adicionar um novo objeto create a new variable to <RadzenTree Data="HERE"