简体   繁体   中英

WPF TreeView and Virtualization(UI & Data)

I have a hierarchical structure such as the following :

 class User
  {
     public List<Contract> Contracts{get;set;}
  }

  class Contract
  {
     public List<Group> Groups{get;set;}
  }

  class Group
  {
     public List<Article> Articles{get;set;}
  }

  class Article
  {
      public string Name{get;set;}
      public string Id{get;set;}
  }

every user has some contracts associated to him, every contract has some groups associated to him, and every group has some articles.

I represent this visually using a TreeView (with checkboxes such as in http://www.codeproject.com/KB/WPF/TreeViewWithCheckBoxes.aspx ) in WPF :

(every user clicked on displays this structure in a TreeView) :

>Contract1
  >Group1
    >Article1
    >Article2
    >Article3
    >Article4
    ...
  >Group2
  >Group3
  ------------
  Add Button
  Remove Button
  -------------

Using the precedent screen, the user can Add further Articles or Contracts or Groups to his account just by checking on them and clicking Add button or remove them by clicking Remove Button.

the problem is that there are tens of thousands of articles in every Group and I don't want to bring them all in memory at once, for it would slow things down.

Can you think of any better way of handling this ?

Since WPF 3.5 SP1 there is VirtualizationMode.Recycling virtualization mode, which allows reuse item containers and do not create new one each time user scrolling an Items Control.

When VirtualizationMode is set to Recycling, the VirtualizingStackPanel reuses item containers instead of creating a new one each time.

Try out

<TreeView
    VirtualizingStackPanel.IsVirtualizing = "True"
    VirtualizingStackPanel.VirtualizationMode = "Recycling" />

To make it work for TreeView see this SO post: ( I would not copy/paste XAML ) VirtualizingStackPanel on a Treeview ist not Virtualizing

Helpful MSDN link:

The article you refer to is from Josh Smith. This author has another article on "The Code Project" introducing dummy childs and lazy loading integrated in the MVVM pattern to avoid the initial loading of all data.

http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM