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 577031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:06:35+00:00 2026-05-13T14:06:35+00:00

I have a TreeView that is bound to a tree of ViewModel instances. The

  • 0

I have a TreeView that is bound to a tree of ViewModel instances. The problem is that the model data is coming from a slow repository so I need data virtualization. The list of sub ViewModel below a node should only be loaded when the parent tree view node is expanded and it should be unloaded when it is collapsed.

How can this be implemented while adhering to MVVM principles? How can the ViewModel get notified that it needs to load or unload subnodes? That is when a node was expanded or collapsed without knowning anything about the treeview’s existence?

Something makes me feel that data virtualization doesn’t go well with MVVM. Since in data virtualization the ViewModel generally needs to know quite a lot about the current state of the UI and aslo needs to control quite a lot of aspects in the UI. Take another example:

A listview with data virtualization. The ViewModel would need to control the length of the ListView’s scrollthumb since it depends on the number of items in the Model. Also when the user scrolls, the ViewModel would need to known to what position he scrolled to and how big the listview is (how many items currently fit in) to be able to load the right portion of Model data form the repository.

  • 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-13T14:06:36+00:00Added an answer on May 13, 2026 at 2:06 pm

    The easy way to solve this is with a “virtualizing collection” implementation that maintains weak references to its items along with an algorithm for fetching / creating items. The code for this collection is rather complex, what with all the interfaces required and the data structures to efficiently track ranges of loaded data but here is a partial API for a class that virtualized based on indexes:

    public class VirtualizingCollection<T>
      : IList<T>, ICollection<T>, IEnumerable<T>,
        IList, ICollection, IEnumerable,
        INotifyPropertyChanged, INotifyCollectionChanged
    {
      protected abstract void FetchItems(int requestedIndex, int gapStartIndex, int gapEndIndex);
      protected void RecordFetchedItems(int startIndex, int count, IEnumerable items) ...
      protected void RecordInsertOrDelete(int startIndex, int countPlusOrMinus) ...
      protected virtual void OnCollectionChanged(CollectionChangedEventArgs e) ...
      protected virtual void Cleanup();
    }
    

    The internal data structure here is a balanced tree of data ranges, with each data range containing a start index and an array of weak references.

    This class is designed to be subclassed to provide the logic for actually loading the data. Here is how it works:

    • In the subclass constructor, RecordInsertOrDelete is called to set the initial collection size
    • When an item is accessed using IList/ICollection/IEnumerable, the tree is used to find the data item. If found in the tree and there is a weak reference and the weak reference still points to a life object, that object is returned, otherwise it is loaded and returned.
    • When an item needs to be loaded, an index range is computed by searching forward and back though the data structure for the next/previous already-loaded item, then the abstract FetchItems is called so the subclass can load the items.
    • In the subclass FetchItems implementation, items are fetched and then RecordFetchedItems is called to update the tree of ranges with the new items. Some complexity here is required to merge adjacent nodes to prevent too much tree growth.
    • When the subclass gets a notification of external data changes it can call RecordInsertOrDelete to update the index tracking. This updates start indexes. For an insert, this may also split a range, and for a delete this may require one or more ranges to be recreated smaller. This same algorithm is used internally when items are added / deleted through the IList and IList<T> interfaces.
    • The Cleanup method is called in the background to incrementally search the tree of ranges for WeakReferences and whole ranges that can be disposed, and also for ranges that are too sparse (eg only one WeakReference in a range with 1000 slots)

    Note that FetchItems is passed a range of unloaded items so it can use a heuristic to load multiple items at once. A simple such heuristic would be loading the next 100 items or up to the end of the current gap, whichever comes first.

    With a VirtualizingCollection, WPF’s built-in virtualization will cause data loading at the appropriate times for ListBox, ComboBox, etc, as long as you are using eg. VirtualizingStackPanel instead of StackPanel.

    For a TreeView, one more step is required: In the HierarchicalDataTemplate set a MultiBinding for ItemsSource that binds to your real ItemsSource and also to IsExpanded on the templated parent. The converter for the MultiBinding returns its first value (the ItemsSource) if the second value (the IsExpanded value) is true, otherwise it returns null. What this does is makes it so that when you collapse a node in the TreeView all references to the collection contents are immediately dropped so that VirtualizingCollection can clean them up.

    Note that virtualization need not be done based on indexes. In a tree scenario, it can be all-or-nothing, and in a list scenario an estimated count can be used and ranges filled in as necessary using a “start key” / “end key” mechanism. This is useful when the underlying data may change and the virtualized view should track its current location based on which key is at the top of the screen.

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

Sidebar

Related Questions

I have a TreeView that is bound to an ObservableCollection in my ViewModel. I
I have a TreeView implemented in WPF that is bound to some XML data
I have a treeview that is bound to a collection and each item in
I have a TreeView that is bound to a XmlDataSource control. I've added some
I have a TreeView that is bound to a collection class that I have
I have a TreeView in master page that is bound on each page and
In Silverlight 3 I have a TreeView bound to an ObservableCollection in my ViewModel.
I have a bound treeview with an ItemsControl that dynamically creates the treeview items
I have a treeview that is bound to a database table. Each treeview item
Scenario I have a TreeView that is bound to ObservableCollection<T> . The collection gets

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.