How To Load Child Items On Demand In WPF Treeview?

Sample date Updated on Dec 22, 2025
childnodes ondemand treeview wpf wpf-application wpf-controls wpf-treeview

This repository describes how to load child items on demand in WPF TreeView (SfTreeView).

You can load child items for the node in Execute method of LoadOnDemandCommand. Execute method will get called when user expands the tree node. In LoadOnDemand.Execute method, you have can perform following operations,

  • Show or hide busy indicator in the place of expander by setting TreeViewNode.ShowExpanderAnimation until the data fetched.
  • Once data fetched, you can populate the child nodes by calling TreeViewNode.PopulateChildNodes method by passing the child items collection.
  • When load on-demand command executes expanding operation will not be handled by TreeView. So, you have to set TreeViewNode.IsExpanded property to true to expand the tree node after populating child nodes.
  • You can skip population of child items again and again when every time the node expands, based on TreeViewNode.ChildNodes count.
/// <summary>
/// Execute method is called when any item is requested for load-on-demand items.
/// </summary>
/// <param name="obj">TreeViewNode is passed as default parameter </param>
private void ExecuteOnDemandLoading(object obj)
{
    var node = obj as TreeViewNode;

    // Skip the repeated population of child items when every time the node expands.
    if (node.ChildNodes.Count > 0)
    {
        node.IsExpanded = true;
        return;
    }

    //Animation starts for expander to show progressing of load on demand
    node.ShowExpanderAnimation = true;
    var sfTreeView = Application.Current.MainWindow.FindName("sfTreeView") as SfTreeView;
    sfTreeView.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
    {
        currentNode = node;
        timer.Start();
    }));
}

Showing BusyIndicator when expanding the TreeView

Up arrow