Creating TreeView using Grid in Silverlight (Component One – Cell Factory) – I

[Component one already have a new attribute in C1FlexGrid ‘ ChildItemsPath="Children" which will create the tree view look and feel without any of this custom cell factory. I did it anyway as a means to learn.]

Silverlight 4 tree view have one performance problem, when you try to expand a node with 1000 odd children, it takes close to 10 to 14 second to load. But it is only the initial cost, once the data loaded into memory, when you expand or collapse the same node it expands quickly. If you have many number of nodes with 1000s of children then you are going incur the cost of each node expansion. As you can imagine it does not go very well with the end users. For starters, it will not go past QA when they see that bad of performance. So I was thinking about adopting data grid to display the same content, yet make it look like tree view. The reason I chose grid instead of any other controls is virtualization. I tested a simple grid with same amount of data in it and it was simply amazing. It was so fast. You can’t time it since it expand and collapse instantly.

I have been using Component One control lately for our development and I very comfortable using it. I decided to develop the grid using C1FlexGrid and one of its power feature called cell factory. I never really appreciated the full power of  till I attempted to solve my problem using cell factory. I decided to blog about it as a document of this feature and as a reference for future. The best reference if you want to learn how the cell factory can be used, I would recommend you to get to Component One web site and look for Silverlight FlexGrid samples. There is also a PDF documentation explaining in detail how to use cell factory with an iTunes like grid.

Ok, that is enough talk, lets see the problem at hand. Let me first show, what am I trying to solve and then we see the code.

image

image

The first picture shows the parents collapsed and the second picture shows the first parent expanded. To make this exercise simple, I have only two level. There are 1000 parents and every even parent have 500 children. Lets look at the class definition for the collection. The class is called Person and shown as follows

 1: public class Person:INotifyPropertyChanged
 2: {
 3:     public string Name { get; set; }
 4:     public int Age { get; set; }
 5:     public bool TwoState { get; set; }
 6:     public ObservableCollection<Person> Children { get; set; }
 7:    8:     public Person()
 9:     {
 10:         TwoState = false;
 11:         Children = new ObservableCollection<Person>();
 12:     }
 13:    14:     public event PropertyChangedEventHandler PropertyChanged;
 15:     private void NotifyPropertyChanged(string info)
 16:     {
 17:         if (PropertyChanged != null)
 18:        {
 19:             PropertyChanged(this, new PropertyChangedEventArgs(info));
 20:        }
 21:     }
 22: }

Following code load the data to be displayed on the treeview

 1: private void LoadPeople()
 2: {
 3:      _people = new List<Person>();
 4:      for (int i = 0; i < 1000; i++)
 5:      {
 6:           Person p1;
 7:           if (i % 2 == 0)
 8:          {
 9:               p1 = new Person() { Name = "parent" +i.ToString(), Age = 20 };
 10:               for (int j = 0; j < 500; j++)
 11:                  p1.Children.Add(new Person() { Name = "Child" + j.ToString(), Age = 10 });
 12:          }
 13:          else
 14:          {
 15:              p1= new Person() { Name = "Parent" + i.ToString(), Age = 20 };
 16:          }
 17:          _people.Add(p1);
 18:     }
 19: }

As one of my friend pointed out, it is better to use string.format rather than ‘+’ in string operations. Now lets look at the XAML which does the actual work of showing the data

 1: <controls:TreeView Grid.Column="1" VerticalAlignment="Stretch" VirtualizingStackPanel.VirtualizationMode="Standard"
 2:                                    ItemsSource="{Binding People}" >
 3:      <controls:TreeView.ItemTemplate>
 4:           <common:HierarchicalDataTemplate ItemsSource="{Binding Children}">
 5:                 <StackPanel>
 6:                     <Grid>
 7:                         <Grid.ColumnDefinitions>
 8:                             <ColumnDefinition Width="10*"/>
 9:                             <ColumnDefinition Width="90*"/>
 10:                          </Grid.ColumnDefinitions>
 11:                          <CheckBox IsChecked="{Binding TwoState}" Grid.Column="0"/>
 12:                             <TextBlock Grid.Column="1" Text="{Binding Name}"/>
 13:                       </Grid>
 14:                  </StackPanel>
 15:            </common:HierarchicalDataTemplate>
 16:        </controls:TreeView.ItemTemplate>
 17: </controls:TreeView>

With these three set of code, I am able to generate the tree view using toolkit tree view control. When you run the code initial load takes almost 30 to 35 seconds to load and then each child expand and collapse takes around 10 to 15 seconds to load. Once children are loaded in memory, future expand and collapse are very fast. The main problems are initial load and time to expand each node.

Now we know the problem at hand, we can look at how we can use C1FlexGrid to create tree view look and feel.

Advertisements

3 thoughts on “Creating TreeView using Grid in Silverlight (Component One – Cell Factory) – I

  1. Pingback: Creating TreeView using Grid in Silverlight (Component One – Cell Factory) – II « Unni's space

  2. Pingback: Creating TreeViews using FlexGrid for Silverlight - Silverlight & WPF - Silverlight - ComponentOne Community

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s