The issue manifests both when
Happens in 2.14.1 first.
*** Thread created by admin on customer behalf ***
Hello,
Generally, creating a new collection for its Data will change its reference, so the framework will call the OnParametersSet method of the treeview and it will update. The alternative is to use an ObservableCollection and its Add(), Remove() and Clear method().
The bug this page tracks is that neither of these approaches works.
A workaround could be to toggle its visibility so it re-renders completely anew with the new data:
@using System.Collections.ObjectModel
<TelerikButton OnClick="@AddChildClick">Add Child</TelerikButton>
@if (TreeViewVisible)
{
<TelerikTreeView Data="@ProductTypesFlatData">
<TreeViewBindings>
<TreeViewBinding ParentIdField="ParentIdValue" ExpandedField="Expanded"></TreeViewBinding>
</TreeViewBindings>
</TelerikTreeView>
}
@code{
bool TreeViewVisible { get; set; } = true;
async Task RepaintTree()
{
TreeViewVisible = false;
await Task.Delay(30);
TreeViewVisible = true;
StateHasChanged();
}
private ObservableCollection<TreeItem> ProductTypesFlatData { get; set; }
private int _childCounter = 1;
protected override void OnInitialized()
{
ProductTypesFlatData = new ObservableCollection<TreeItem>();
ProductTypesFlatData.Add(new TreeItem() { Expanded = true, HasChildren = true, Icon = IconName.Folder, Id = "1", ParentIdValue = null, Text = "Parent1" });
AddChild();
AddChild();
AddChild();
}
private async Task AddChildClick()
{
AddChild();
await RepaintTree();
}
private void AddChild()
{
ProductTypesFlatData.Add(new TreeItem() { Expanded = false, HasChildren = false, Icon = IconName.Gear, Id = $"1.{_childCounter.ToString()}", ParentIdValue = "1", Text = $"Child{_childCounter}" });
_childCounter++;
}
public class TreeItem
{
public string Id { get; set; }
public string Text { get; set; }
public string ParentIdValue { get; set; }
public bool HasChildren { get; set; }
public string Icon { get; set; }
public bool Expanded { get; set; }
}
}
Regards,
Marin Bratanov
Progress Telerik
Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).
Anyway to refresh the TreeView manually?