title: Lazy tree loading in WPF
body: <p>I am trying to implement lazy tree loading in WPF. I have a <code>TreeView</code> which I populate with some nodes. Each node has a subnode with a "Loading..." header. What I want to achieve is:</p>
  
  <ol>
  <li>User expands top node.</li>
  <li>Top node expands and shows the "Loading..." subnode.</li>
  <li>After the loading node is shown, method <code>foo</code> determines what subnodes to be added.</li>
  <li>When the work is done, the "Loading..." node is removed and the new nodes which method <code>foo</code> has created are shown.</li>
  </ol>
  
  <p>The idea is to send feedback to the user that the tree is loading since it takes quite some time for my <code>foo()</code> method to run.</p>
  
  <p>The code which I have is this:</p>
  
  <pre><code>&lt;TreeView x:Name="MyTree" TreeViewItem.Expanded="MyTreeItem_Expanded" ItemsSource="{Binding}"&gt;
  
  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
  }
  </code></pre>
  
  <p>With this implementation, when I click on the node, the tree freezes and the node expands only after <code>foo()</code> is done and "Loading..." is removed.</p>
  
  <p>How can I first show the "Loading..." node and then do the loading?</p>