DEV Community

Sven Glöckner
Sven Glöckner

Posted on

Refreshing DxTreeView items and keep selection

Today, I want to refresh the treeview items in a DevExpress Blazor DxTreeView.

Imagine you edited one of the node's items and want to refresh the whole tree without loosing the selected node.

My DxTreeView

<DxTreeView @ref="@_treeView" AllowSelectNodes="true" SelectionChanged=@(async e => await SelectChildNodeAsync(e))
    Data=@TreeViewItems
    NameExpression="@(dataItem => ((Model.TreeViewItem)dataItem).Id.ToString())"
    TextExpression="@(dataItem => ((Model.TreeViewItem)dataItem).Name)"
    ChildrenExpression="@(dataItem => ((Model.TreeViewItem)dataItem).Items)">
</DxTreeView>

After editing finished, you could programmatically refresh the treeview's datasource, expand and select the current node with the help of the API:

TreeViewItems = await DataAccess.GetTreeViewData();
await InvokeAsync(StateHasChanged);
_treeView.ExpandToNode(nodeInfo => nodeInfo.Name == DetailItem.Id.ToString());
_treeView.SelectNode(nodeInfo => nodeInfo.Name == DetailItem.Id.ToString());

See also the issue on GitHub.

Top comments (0)