Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:50:09+00:00 2026-05-31T09:50:09+00:00

I have a WPF application using MVVM that utilises TreeViewItemViewModels to simplify working with

  • 0

I have a WPF application using MVVM that utilises TreeViewItemViewModels to simplify working with a treeview as demonstrated by Josh Smith http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

My treeview represents the records in a relational database and each node has its children loaded on demand. To improve the responsiveness of the UI I’ve been attempting to load the data asychronously using Tasks in .net 4.0 but am struggling to implement this satisfactorily.

The treeviewitemviewmodel IsExpanded property looks like this:

public bool IsExpanded
{
  get { return _isExpanded; }
  set
  {        
    if (_isExpanded == value)
      return;

    if (!_isSelected)       
      IsSelected = true;

    _isExpanded = value;

    if (_isExpanded &&
        _parent != null &&
        _parent.GetType() != typeof(FirstGenerationViewModel))
      _parent.IsExpanded = true;

    ApplicationSetup.Messenger.NotifyColleagues(ApplicationSetup.MSG_WORKING, true);

    Task task = Task.Factory.StartNew(() =>
    {
      LoadChildren();
    })
    .ContinueWith((t) => 
      {
        this.OnPropertyChanged("IsExpanded");
        ApplicationSetup.Messenger.NotifyColleagues(ApplicationSetup.MSG_WORKING, false);            
      }, ApplicationSetup.UIScheduler);        
  }
}

If I select items manually and expand and collapse them all works as expected – a working animation is displayed, whilst LoadChildren() executes and then the items expand when OnPropertyChanged is called and the animation closes.

However, I’m trying to implement a search function. If a record is already loaded, I simply iterate the treeview until its found and then select it. However, if its not loaded the process should search the database for a record then display it. As records are loaded on demand, the process gets the sought item and its ancestors from the db and attempts to expand each ancestor in turn then finally selecting the sought item:

      foreach (var item in ancestors.Keys)
      {            
        TreeViewItemViewModel tv = _fgvm.recordsViewHelper.GetTreeViewItem(new ItemArgs 
                                                                            { 
                                                                              TargetTableName = item, 
                                                                              TargetPk = ancandtarget[item]
                                                                            });

        tv.IsExpanded = true;            
      }

If the sought item is high in the hierarchy, this works. However, when the record is nested a couple of items down, tv is null. I think this is because the item hasn’t loaded before _fgvm.recordsViewHelper.GetTreeViewItem tries to locate it in the treeview.

How can I accomplish this? I’ve tried using Wait() but seem to freeze the UI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:50:10+00:00Added an answer on May 31, 2026 at 9:50 am

    Solved this myself. Obvious really. I now use a wrapper method to expand the ancestors in the foreach loop which returns the task used by the IsExpanded property. This task can then have Wait() called on it which ensures the parents of the next ancestor are fully loaded before continuing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application using MVVM. I have some user controls that show
I have a WPF application implemented using the MVVM framework that uses an ActiveX
I have a WPF/C# application that is using the MVVM light framework. When it
I'm using MVVM in my WPF application. I have a Window that has a
I have a WPF Application using Fluent NHibernate 1.0 RTM and System.Data.SQLite 1.0.65 that
I have a WPF application that is using a WindowsFormsHost control to host a
I'm going to do a wpf application using MVVM(It based on http://www.codeproject.com/KB/WPF/MVVMQuickTutorial.aspx ). This
I have a WPF Microsoft Surface Application and I'm using MVVM-Pattern. I have some
I am working on a WPF application with MVVM, but am not using any
I have an application all done in WPF, using MvvM/Prism/Unity and Remote as datasource.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.