My scenario is this:
I have a client-server application with a tree (RadTreeView), which is data bound with a DataTemplate for the nodes.
Nodes that are not yet expanded don't hold any data, but an invisible child node, so an expander will be displayed.
Expanding the node loads the next level from the server, then removes the child node from the ItemsSource and then adds the new nodes to the ItemsSource.
The WPF application is designed as MVVM.
Since the application has more windows besides the tree, and the response from the server might take a while, the application should not wait for the server. Thus we decided to use a BackgroundWorker in the view model.
private void LoadChildrenBackground()
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += (sender, args) => _node.Expand();
backgroundWorker.RunWorkerCompleted += (sender, args) =>
Children.Clear(); //or Children.Remove(DummyChild); <- here is there problem!
foreach (ITreeNode child in _node.Children)
Children.Add(new TreeViewItemViewModel(child, this, true));
When I run the program, and expand a few nodes by clicking the expander, the tree runs into a NullReferenceException.
I get this call stack location:
c:\TB\102\WPF_Scrum\Release_WPF\Sources\Development\Controls\Navigation\TreeView\Virtualization\TreeViewPanel.cs' Line 1452
This happens only when a level has more than 405 nodes, and when the background worker is used, and when I remove the dummy child. So I suspect it's a matter of the asynchronous behaviour when removing an Item from the ItemsSource.
I have tried removing the dummy child before and after inserting the new nodes, even after the background worker is finished, it all ends up in the exception.
I can provide a simple example in a Visual Studio solution which demonstrates the effect (17kB zip).
Maybe your developers are interested in the exception, or maybe you have a workaround(?)
I hope you can help me, because when I get the RadTreeView running with our MVVM pattern, my evaluation will be (positively!) complete. Seems like this is the last issue I have :)