This is a migrated thread and some comments may be shown as answers.

Exception in MVVM using BackgroundWorker in the ViewModel

4 Answers 416 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 12 Sep 2012, 09:34 AM
Hello Telerik,

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));
   };
   backgroundWorker.RunWorkerAsync();
}

Effect:
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 :)

4 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 14 Sep 2012, 09:30 AM
Hello Martin,

In order to better understand the source for the issue, we'd really need to see your implementation and a sample solution reproducing the exception. However, looking at your requirements, I was wondering if you have tried the RadTreeView.LoadOnDemand functionality. It allows you to load the children items on demand out-of-the-box without the need of a dummy item. It also allows you to display a loading indicator on top of the expander icon while the children are loading thus visualizing the load process.

Since you're using MVVM, you can bind the event to a command (for example using the MVVMLight EventToCommand behavior). As a whole, the LoadOnDemand feature should allow you to implement your scenario without leading to any issues - please examine the TreeView Load On Demand databinding example in our WPF demos.

All the best,
Tina Stancheva
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Martin
Top achievements
Rank 1
answered on 01 Oct 2012, 11:02 AM
Hello Tina,

sorry for the late reply, I have been busy on another project.
I have a sample solution reproducing the issue. The zip-File is 17kb without the Telerik ddls, How can I send it to you?

I will try the load on demand now, thank you for suggesting!
0
Tina Stancheva
Telerik team
answered on 03 Oct 2012, 09:10 AM
Hello Martin,

You can start a support ticket through the support ticketing system (http://www.telerik.com/account/support-tickets/available-support-list.aspx) where you can attach your sample solution.

All the best,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Martin
Top achievements
Rank 1
answered on 08 Oct 2012, 09:36 AM
Okay, I have been able to submit a support ticket now.
With the LoadOnDemand of the RadTreeView feature I have managed to load my data on expanding a node in a BackgroundWorker in the view model, so I am fine now.
Everything is working like I hoped it would, so thank you very much for your support!
Tags
TreeView
Asked by
Martin
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Martin
Top achievements
Rank 1
Share this question
or