Exception in MVVM using BackgroundWorker in the ViewModel

5 posts, 0 answers
  1. Martin
    Martin avatar
    7 posts
    Member since:
    Jun 2012

    Posted 12 Sep 2012 Link to this post

    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 :)
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 14 Sep 2012 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Martin
    Martin avatar
    7 posts
    Member since:
    Jun 2012

    Posted 01 Oct 2012 Link to this post

    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!
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 03 Oct 2012 Link to this post

    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.

  6. Martin
    Martin avatar
    7 posts
    Member since:
    Jun 2012

    Posted 08 Oct 2012 Link to this post

    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!
Back to Top
UI for WPF is Visual Studio 2017 Ready