Hy all,
I work on a server-side blazor solution. I want to add nodes to a treeview by code. My Treeview component implementation is below:
<SfTreeView @ref="treeView" TValue="CommessaElementVM" AllowDragAndDrop="true">
<TreeViewFieldsSettings DataSource="@vm.ElementListVM" Id="RowGuid" Text="Descrizione"
Child="@("Children")" TValue="CommessaElementVM" Selected="IsSelected"
Expanded="Expanded"></TreeViewFieldsSettings>
<TreeViewEvents TValue="CommessaElementVM" NodeSelected="OnSelect" NodeExpanding="OnExpanding" NodeDropped="OnNodeDropped" />
</SfTreeView>
While CommessaElementVM class is:
public class CommessaElementVM
{
public string RowGuid { get; set; }
public string GuidParent { get; set; }
public string Descrizione { get; set; }
public bool Expanded { get; set; }
public bool IsSelected { get; set; }
public bool HasChildren { get; set; }
public bool IsRootElement => string.IsNullOrEmpty(GuidParent);
private List<CommessaElementVM> _Children = new List<CommessaElementVM>();
public List<CommessaElementVM> Children
{
get
{
return _Children;
}
set
{
HasChildren = true;
_Children = value;
}
}
}
vm.ElementListVM is a List<CommessaElementVM> implemented in my MVVM viewmodel.
As you can see data Nodes is loaded on demand by code. When I want to add a node I use a SfDialog to force user to insert others data. When User click on Save button of SfDialog my code insert a node. If node is child of an existing parent node all work fine and child node is showed on treeview. This is not true if I try to add a root Node. In this case I add a new element in vm.ElementListVM (DataSource) but this node is not visible on treeview. I tried with treeView.RefreshNode(..), treeView.Refresh(), StateHasChanged methods but it does not work. The new node showed only if I refresh my browser.
What should I do to add a root node by code?
thanks.
Rolando