Click here to Skip to main content
13,351,985 members (78,032 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 19 Oct 2009

LazyLoad WPF Treeview with Large Amount of Two Level Broad Data

, 29 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Enhance performance of WPF Treeview control while loading large amount of two level broad data


This article will introduce the hand-on experience the author and his team members used to solve the slow performance of WPF Treeview Control which contains two levels of broad data.


Currently, I am working on enhancing performance with a Job page. This page has a two levels depth TreeView control. The top level displays the list of jobs, and the second level displays the number of job associated items.

Here is the initial page of jobs. You can see the job associated items when you expand the job.


In the current application, the numbers of jobs get increased by 10 everyday, and each job has around 150 Job items on average.

This page gets slower and slower as more and more jobs gets created until it reaches the maximum acceptance threshold.

Our development team looked at that page, and we discovered that we load all the jobs and associated job items at once, and passed it to the client, and client uses TreeView and HierarchicalDataTemplate to display the job and job items. As a consequence, the more job and job items get added, the slower is will be for sure.

I did some investigation/Googling/experiments to see if we can lazy load tree without any additional change. I found that the current WPF TreeView control will eager load two level node, i.e., If you expand a node, it will load all the child and grand child nodes, but will not load any node which is below the grand child nodes. It doesn’t help us because we have only two levels of nodes.

After a discussion with team members, we comes up with two possible approaches:

Approach 1: There is no need to use WPF TreeView control since we have only two levels of data. We could use ListView control to display job collection, after user clicks on the job, the associated job item can be retrieved from server, and be displayed.

Approach 2: We still use TreeView control; however, we will overwrite the ExpandEvent. It will allows us to lazyload Job Items. I found a fix from the Microsoft website here.

Approach 2 was chosen since we would like to make a minimum change to the current application.

Using the Code

In the example, you can switch between my original implementation and the performance improved implementation. You can open the App.xaml, and change the StartupUri value. If StartupUrl="Job.xaml", then it is the original implementation, and the performance is poor, if StartupUrl="LazyJob.xaml", then the performance is very fast.

Here is the sample code for approach 2:


Job UI xaml page hooks up two events, Loaded and Unloaded event. It also defines two templates, JobItemTemplate and JobTemplate. Each template contains a checkbox control and a TextBlock control. Xaml page also has a empty TreeView which will be updated later dynamically.

<Window x:Class="JobLazyLoad.LazyJob"

    Loaded="OnLoaded" Unloaded="OnUnloaded"> 
        <DataTemplate x:Key = "JobItemTemplate" >
           <CheckBox IsChecked="{Binding Path=IsSelected}">
                <TextBlock Text="{Binding Path=Name}"/>
        <DataTemplate x:Key = "JobTemplate">

            <CheckBox IsChecked="{Binding Path=IsSelected}">
                 <TextBlock Text="{Binding Path=Name}"/>
        <TreeView x:Name="JobTree"/> 

Here is Job.xmls.cs code behind file.

While the page gets loaded, it hooks up TreeViewItem expand event with customized eventhandler (OnTreeItemExpanded), it also loads the first level of node (LoadRootNodes). Once use clicks and expands on the tree, the OnTreeItemExpanded gets invoked, and it loads the associated JobItemCollection.

public partial class LazyJob : Window
     private object _dummyNode = null;
     private JobViewModel _viewModel;
     public LazyJob()

     private void InitializeViewModel()
          _viewModel = new JobViewModel();
          this.DataContext = _viewModel;

     private void OnLoaded(object sender, RoutedEventArgs e)
              new RoutedEventHandler(OnTreeItemExpanded)); 


     private void OnUnloaded(object sender, RoutedEventArgs e)
              new RoutedEventHandler(OnTreeItemExpanded)); 

     private void OnTreeItemExpanded(object sender, RoutedEventArgs e)
         TreeViewItem item = e.OriginalSource as TreeViewItem;
         if (item != null && item.Items.Count == 1 && item.Items[0] == _dummyNode) 
             Domain.Job job = item.Header as Domain.Job;
             foreach (var acquisitionItem in _viewModel.JobItemCollection)
                  TreeViewItem subItem = new TreeViewItem();
                  subItem.Header = acquisitionItem;
                  subItem.HeaderTemplate = FindResource(
                      "JobItemTemplate") as DataTemplate;

     private void LoadRootNodes()
         foreach (Domain.Job job in _viewModel.LazyJobCollection)
             TreeViewItem item = new TreeViewItem();
             item.Header = job;
             item.HeaderTemplate = FindResource("JobTemplate") as DataTemplate; 

JobViewModel class has two methods, one of loading only the Job object collection, and another method will load JobItemCollection for expanded job.

public class JobViewModel
    public int CurrentJobId { get; set; }

    public IList<Domain.Job> LazyJobCollection
        get { return new JobService().GetLazyJobCollection(); }

    public IList<JobItem> JobItemCollection
        get { return new JobService().GetJobItemCollection(CurrentJobId); } 

That is it. In approach 2, we load job items only when we need it. After implementing the change, it becomes about 150 times faster to get this page displayed.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Young Ye
Software Developer (Senior)
Canada Canada
I am a passionated software developer / engineer with strong desire to develop in a simple, fast, beautiful way with the skillset such as simple design, refactoring, TDD. i worked in J2EE for about 5 years, now i work as a .NET devleoper.

You may also be interested in...


Comments and Discussions

Suggestion[My vote of 2] Could u realize it for XML file? Pin
dimas197130-Jan-12 7:30
memberdimas197130-Jan-12 7:30 
GeneralRe: [My vote of 2] Could u realize it for XML file? Pin
Young Ye1-Feb-12 4:25
memberYoung Ye1-Feb-12 4:25 
GeneralMy vote of 3 Pin
Poiuy Terry31-May-11 16:17
memberPoiuy Terry31-May-11 16:17 
GeneralRe: My vote of 3 Pin
Young Ye29-Dec-11 6:14
memberYoung Ye29-Dec-11 6:14 
GeneralMy vote of 4 Pin
team a14-Jul-10 9:30
memberteam a14-Jul-10 9:30 
GeneralRe: My vote of 4 Pin
Young Ye29-Dec-11 6:13
memberYoung Ye29-Dec-11 6:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 29 Feb 2012
Article Copyright 2009 by Young Ye
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid