I am trying to implement lazy tree loading in WPF. I have a TreeView which I populate with some nodes. Each node has a subnode with a "Loading..." header. What I want to achieve is:
- User expands top node.
- Top node expands and shows the "Loading..." subnode.
- After the loading node is shown, method
foodetermines what subnodes to be added. - When the work is done, the "Loading..." node is removed and the new nodes which method
foohas created are shown.
The idea is to send feedback to the user that the tree is loading since it takes quite some time for my foo() method to run.
The code which I have is this:
<TreeView x:Name="MyTree" TreeViewItem.Expanded="MyTreeItem_Expanded" ItemsSource="{Binding}">
private void MyTreeItem_Expanded(object sender, RoutedEventArgs e) {
// some logic to figure out from which node the event has been risen
// method foo adds the new subnodes
// code which removes the "Loading..." subnode
}
With this implementation, when I click on the node, the tree freezes and the node expands only after foo() is done and "Loading..." is removed.
How can I first show the "Loading..." node and then do the loading?