New to Telerik UI for Blazor? Download free 30-day trial

TreeView - Refresh Data

The most common reason you would use an ObservableCollection is to make a component (like a grid, treeview, treelist, dropdown) change or react when you change that collection.

When you want to refresh the component data source like that, there are two important framework behaviors you need to be aware of - when ObservableCollection instances fire events, and how to refresh the data of a component when it is not an observable collection.

In this article:

Observable Data

Databound components can benefit from live data - when the data source collection changes, the components should update to reflect that change. Most data-bound components in the Telerik UI for Blazor suite implement such functionality.

When the Data of the component is a collection that implements the INotifyCollectionChanged interface (such as ObservableCollection), the Telerik components subscribe to its CollectionChanged event to update. This means that adding items, removing items, or clearing the collection updates the components (its .Add(), .Remove() and .Clear() methods).

The Observable collections fire the CollectionChanged event only when their Add, Remove and Clear methods are called. They do not fire it when you change the value of a field of one of their elements.

Bind the TreeView to an ObservableCollection, so it can react to collection changes.

@* Add/remove item to see how the TreeView reacts to that change. *@

@using System.Collections.ObjectModel

<TelerikButton OnClick="@AddItem">Add item</TelerikButton>

<TelerikButton OnClick="@RemoveItem">Remove item</TelerikButton>

<TelerikTreeView Data="@Items">
    <TreeViewBindings>
        <TreeViewBinding IdField="Id" ParentIdField="ParentIdValue" ExpandedField="Expanded" TextField="Text" HasChildrenField="HasChildren" IconField="Icon" />
    </TreeViewBindings>
</TelerikTreeView>

@code {
    void AddItem() {
        Items.Add(
            new TreeItem
            {
                Id = Items.Count + 1,
                Text = "Testing",
                ParentIdValue = 1,
                HasChildren = false,
                Icon = "gears",
                Expanded = false
            });
    }

    void RemoveItem()
    {
        if (Items.Count > 0)
        {
           Items.RemoveAt(Items.IndexOf(Items.Last()));
        }
    }

    public class TreeItem
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public int? ParentIdValue { get; set; }
        public bool HasChildren { get; set; }
        public string Icon { get; set; }
        public bool Expanded { get; set; }
    }

    public ObservableCollection<TreeItem> Items { get; set; } = new ObservableCollection<TreeItem>() {

        new TreeItem()
        {
            Id = 1,
            Text = "Project",
            ParentIdValue = null,
            HasChildren = true,
            Icon = "folder",
            Expanded = true
        },
       new TreeItem()
       {
            Id = 2,
            Text = "Design",
            ParentIdValue = 1,
            HasChildren = true,
            Icon = "brush",
            Expanded = true
        },
        new TreeItem()
        {
            Id = 3,
            Text = "Implementation",
            ParentIdValue = 1,
            HasChildren = true,
            Icon = "folder",
            Expanded = true
        },
        new TreeItem()
        {
            Id = 4,
            Text = "site.psd",
            ParentIdValue = 2,
            HasChildren = false,
            Icon = "psd",
            Expanded = true
        },
        new TreeItem()
        {
            Id = 5,
            Text = "index.js",
            ParentIdValue = 3,
            HasChildren = false,
            Icon = "js"
        },
        new TreeItem()
        {
            Id = 6,
            Text = "index.html",
            ParentIdValue = 3,
            HasChildren = false,
            Icon = "html"
        },
        new TreeItem()
        {
            Id = 7,
            Text = "styles.css",
            ParentIdValue = 3,
            HasChildren = false,
            Icon = "css"
        }
    };
}

If you need to add/remove many items to/from the collection, consider creating a new collection and provide its reference to the data parameter. Thus, the component will re-render only once (when the data collection reference is changed) instead of re-rendering multiple times in response to the Add/Remove events.

New Collection Reference

In Blazor, the framework will fire the OnParametersSet event of a child component (which is how child components can react to outside changes) only when it can detect a change in the object it receives through the corresponding parameter (like Data for the data sources of Telerik components). This detection works as follows:

  • For primitive types (such as numbers, strings), this happens when their value changes.

  • For complex types (such as data collections like List, or any IEnumerable, and application-specific models/objects), this happens when the object reference changes.

    Thus, you would usually need to create a new reference for the view-model field (such as TreeViewData = new List<MyTreeViewItem>(theUpdatedDataCollection);) when you want the component to update.

Create new collection reference to refresh the TreeView data.

@* Add/remove item or change the collection to see how the TreeView reacts to that change. *@

<TelerikButton OnClick="@AddItem">Add item</TelerikButton>

<TelerikButton OnClick="@RemoveItem">Remove item</TelerikButton>

<TelerikButton OnClick="@ChangeItems">Change items data</TelerikButton>

<TelerikTreeView Data="@Items">
    <TreeViewBindings>
        <TreeViewBinding IdField="Id" ParentIdField="ParentIdValue" ExpandedField="Expanded" TextField="Text" HasChildrenField="HasChildren" IconField="Icon" />
    </TreeViewBindings>
</TelerikTreeView>

@code {
    void AddItem()
    {
        Items.Add(
            new TreeItem
            {
                Id = Items.Count + 1,
                Text = "Testing",
                ParentIdValue = 1,
                HasChildren = false,
                Icon = "gears",
                Expanded = false
            });
        Items = new List<TreeItem>(Items);
    }

    void RemoveItem()
    {
        if (Items.Count > 0)
        {
            Items.RemoveAt(Items.IndexOf(Items.Last()));
            Items = new List<TreeItem>(Items);
        }
    }

    void ChangeItems()
    {
        Items = new List<TreeItem>()
        {
            new TreeItem()
        {
            Id = 1,
            Text = "New root item",
            ParentIdValue = null,
            HasChildren = true,
            Icon = "folder",
            Expanded = true
        },
            new TreeItem()
        {
            Id = 2,
            Text = "New child item 1",
            ParentIdValue = 1,
            HasChildren = true,
            Icon = "brush",
            Expanded = true
        },
            new TreeItem()
        {
            Id = 3,
            Text = "New child item 2",
            ParentIdValue = 1,
            HasChildren = false,
            Icon = "folder",
            Expanded = true
        },
            new TreeItem()
        {
            Id = 4,
            Text = "Child of item 1",
            ParentIdValue = 2,
            HasChildren = false,
            Icon = "gears",
            Expanded = true
        }
        };
        Items = new List<TreeItem>(Items);
    }

    public class TreeItem
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public int? ParentIdValue { get; set; }
        public bool HasChildren { get; set; }
        public string Icon { get; set; }
        public bool Expanded { get; set; }
    }

    public List<TreeItem> Items { get; set; } = new List<TreeItem>() {

        new TreeItem()
        {
            Id = 1,
            Text = "Project",
            ParentIdValue = null,
            HasChildren = true,
            Icon = "folder",
            Expanded = true
        },
       new TreeItem()
       {
            Id = 2,
            Text = "Design",
            ParentIdValue = 1,
            HasChildren = true,
            Icon = "brush",
            Expanded = true
        },
        new TreeItem()
        {
            Id = 3,
            Text = "Implementation",
            ParentIdValue = 1,
            HasChildren = true,
            Icon = "folder",
            Expanded = true
        },
        new TreeItem()
        {
            Id = 4,
            Text = "site.psd",
            ParentIdValue = 2,
            HasChildren = false,
            Icon = "psd",
            Expanded = true
        },
        new TreeItem()
        {
            Id = 5,
            Text = "index.js",
            ParentIdValue = 3,
            HasChildren = false,
            Icon = "js"
        },
        new TreeItem()
        {
            Id = 6,
            Text = "index.html",
            ParentIdValue = 3,
            HasChildren = false,
            Icon = "html"
        },
        new TreeItem()
        {
            Id = 7,
            Text = "styles.css",
            ParentIdValue = 3,
            HasChildren = false,
            Icon = "css"
        }
    };
}

See Also

In this article
Not finding the help you need? Improve this article