Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Silverlight » How to do Paging in a TreeView Control in Silverlight

How to do Paging in a TreeView Control in Silverlight

The TreeView in Silverlight is a powerful way to show hierarchical data. This article describe how to add paging inside the hierarchy of your treeview.

Author Rank :
Page Views : 3154
Downloads : 76
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
RadTreeViewPaging.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


img2.jpg

Figure 1 - Paging in a TreeView in Silverlight

Introduction

One of the unfortunate limitations of the TreeView and RadTreeView (Telerik) in Silverlight is that if you add a few hundred items to a tree node, the expand and collapse of the node become severely sluggish.  One way around this painful symptom is to put paging inside your tree node.  Here is how to do it:

Creating the Data Templates

When using MVVM (Model View ViewModel),  its best to use DataTemplates to resolve the data in the ViewModel.  The TreeView uses something called a HierarchicalDataTemplate to represent the data template at each node level of the tree.The HierarchicalDataTemplate is the same as a data template except it allows you to specify the source and data template of the child nodes. Below are the two HierarchicalDataTemplates for the node structure you see in Figure 1.
 

Listing 1 - HierarchicalDataTemplates for the Customer - Order Tree Structure
 

    <UserControl.Resources>

       
<Controls:HierarchicalDataTemplate x:Key="OrdersTemplate" >
           
<StackPanel Orientation="Horizontal">
               
<TextBlock Text="Order #" />
               
<TextBlock Text="{Binding OrderNumber}" />
           
</StackPanel>
       
</Controls:HierarchicalDataTemplate>
 

        <Controls:HierarchicalDataTemplate x:Key="CustomersTemplate" ItemsSource="{Binding PagedOrders}" ItemTemplate="{StaticResource OrdersTemplate}">

            <StackPanel Orientation="Vertical">
               
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
               
<Controls1:DataPager PageSize="10" Source="{Binding PagedOrders}" />
           
</StackPanel>
       
</Controls:HierarchicalDataTemplate>
   
</UserControl.Resources>

  <Grid x:Name="LayoutRoot" Background="White">
       
<telerik:RadTreeView  ItemsSource="{Binding Customers}" ItemTemplate="{StaticResource CustomersTemplate}"
/>

   
</Grid>

Note that inside the the top level data template (CustomersTemplate), we have a DataPager control.  This control will be used in conjunction with the model's order data to page through the list ten orders at a time.

The View Model

In order for the data to be paged, there is another step.  The orders have to be placed inside of a PagedCollectionView.  Let's take a look at the Customer ViewModel which contains a colleciton of orders.
 

Listing 2 - The Customer ViewModel

using System.Collections.ObjectModel;
using
System.Windows.Data; 

namespace RadTreeViewPaging.ViewModels
{
      
public class Customer
      
{
             
public string Name   { get; set; } 

              public PagedCollectionView PagedOrders { get; set; }
             
public ObservableCollection<Order> Orders { get; set; }

              public Customer(string name)
             
{
                    
Name = name;
                    
Orders = new ObservableCollection<Order>();
                    
PagedOrders = new PagedCollectionView(Orders);
             
}
      
}
}

Notice that inside the Customer ViewModel, we have two properties containing order information.  The Orders property is our collection of all the orders.  This is the collection in which we add new orders coming into our view.  The PagedOrders is a wrapper around the Orders that handles paging inside the DataPager. In other words, the PagedCollectionView and the DataPager work together to handle paging seamlessly.  Simply by wrapping the Orders inside this PagedCollectionView and binding the PagedCollectionView to both the DataPager and the Customer HierarchicalDataTemplate, we get paging behavior in the Customer tree node for all orders.

Conclusion

When you push the limits of third party GUI components, you some times have to come up with clever ways to optimize them for use.  The first question you should always ask yourself when optimizing the UI is, "What snapshot of the information do I really need to render on the screen for the user at any given moment?"   A user of your UI can only see and digest a finite amount of information on the screen, even if the data behind it is astronomical.  Paging is one filtering technique for organizing the data so you only present the user a few pieces of information at a time.  It is not always the optimal technique; the technique used is very domain dependent. Anyway, I think we are all on the same page now, so keep this technique in mind the next time you are displaying large data sets in a TreeView using Silverlight and C#.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mike Gold

Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank, JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently working on a book for using .NET with embedded systems.

He can be reached at mike@c-sharpcorner.com

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Nice article by Mahesh On August 27, 2010
Finally back to Silverlight. Nice to see SL work.
Reply | Email | Modify 
Re: Nice article by Mike On August 27, 2010
Thanks Mahesh,

Planning to hit a lot of Silverlight Articles since that is mostly what I'm doing these days :-)
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.