|
1 | | -# How to load child items on demand in wpf treeview? |
2 | | -This repository describes how to load child items on demand in wpf treeview |
| 1 | +# How to load child items on demand in WPF TreeView |
| 2 | + |
| 3 | +This repository describes how to load child items on demand in [WPF TreeView](https://www.syncfusion.com/wpf-controls/treeview) (SfTreeView). |
| 4 | + |
| 5 | +You can load child items for the node in [Execute](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.routedcommand.execute?view=netframework-4.0) method of `LoadOnDemandCommand`. Execute method will get called when user expands the tree node. In `LoadOnDemand.Execute` method, you have can perform following operations, |
| 6 | + |
| 7 | +* Show or hide busy indicator in the place of expander by setting [TreeViewNode.ShowExpanderAnimation](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeView.Engine.TreeViewNode.html#Syncfusion_UI_Xaml_TreeView_Engine_TreeViewNode_ShowExpanderAnimation) until the data fetched. |
| 8 | +* Once data fetched, you can populate the child nodes by calling [TreeViewNode.PopulateChildNodes](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeView.Engine.TreeViewNode.html#Syncfusion_UI_Xaml_TreeView_Engine_TreeViewNode_PopulateChildNodes_System_Collections_IEnumerable_) method by passing the child items collection. |
| 9 | +* When load on-demand command executes expanding operation will not be handled by TreeView. So, you have to set [TreeViewNode.IsExpanded](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeView.Engine.TreeViewNode.html#Syncfusion_UI_Xaml_TreeView_Engine_TreeViewNode_IsExpanded) property to true to expand the tree node after populating child nodes. |
| 10 | +* You can skip population of child items again and again when every time the node expands, based on [TreeViewNode.ChildNodes](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeView.Engine.TreeViewNode.html#Syncfusion_UI_Xaml_TreeView_Engine_TreeViewNode_ChildNodes) count. |
| 11 | + |
| 12 | +``` csharp |
| 13 | +/// <summary> |
| 14 | +/// Execute method is called when any item is requested for load-on-demand items. |
| 15 | +/// </summary> |
| 16 | +/// <param name="obj">TreeViewNode is passed as default parameter </param> |
| 17 | +private void ExecuteOnDemandLoading(object obj) |
| 18 | +{ |
| 19 | + var node = obj as TreeViewNode; |
| 20 | + |
| 21 | + // Skip the repeated population of child items when every time the node expands. |
| 22 | + if (node.ChildNodes.Count > 0) |
| 23 | + { |
| 24 | + node.IsExpanded = true; |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + //Animation starts for expander to show progressing of load on demand |
| 29 | + node.ShowExpanderAnimation = true; |
| 30 | + var sfTreeView = Application.Current.MainWindow.FindName("sfTreeView") as SfTreeView; |
| 31 | + sfTreeView.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => |
| 32 | + { |
| 33 | + currentNode = node; |
| 34 | + timer.Start(); |
| 35 | + })); |
| 36 | +} |
| 37 | +``` |
0 commit comments