Great Silverlight Competency Test Part 4 (Answer)

iStock_Girl-e-choice-XSmall This is an answer to Part 4 of the Great Silverlight Competency Test. Scoring below. 

The tasks were to demonstrate usage of the DataGrid, DataPager and TreeView. Here is one approach…


I opted to create this in an MVVM structure, but not using an MVVM library.  I began by creating a MainPage.xaml with a DataGrid and a DataPager.  It is important to drag these from the toolbox and not just enter them directly into the Xaml, because dragging them onto either the design form or the Xaml page causes Visual Studio to add the namespace you need (sdk) pointing to 

<sdk:DataGrid Width="Auto"
    ItemsSource="{Binding}" />

    PageSize="10" />

The DataGrid’s ItemSource is set to Binding but with no property; this will cause it to bind to the collection presented by the DataContext.

The DataPager has three properties set

  • DisplayMode which determines how the paging will be displayed
  • Source – set to the same source as the DataGrid’s ItemsSource
  • PageSize – how many records per page


The Data

The data we’ll bind to is the same Customer data I’ve used in previous demonstrations, cut down just a bit here.  There are two classes: Customer, and also CustomerCollection. The latter exists to generate a random collection of 500 Customer objects and to make that collection available as a property.  Both classes are defined in Customer.cs which itself is in the folder Model. Here’s the complete Customer and CustomerCollection source code.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace PagingData.Model
    public class CustomerCollection
        private readonly ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
        public ObservableCollection<Customer> Customers
            get { return _customers; }
        public CustomerCollection()

        public void GenerateCustomers(int howMany)
            var firsts = new List<String>
            var lasts = new List<String>
                            "Van Dam",
            var streets = new List<String>
            var cities = new List<String>
                             "New York",
                             "Oak Hill",
                             "South Falls",
                             "Terra Haute",
            var isp = new List<String>
            var states = new List<String>

            var random = new Random();
            for (var i = 0; i < howMany; i++)

                var first = firsts[random.Next(0, firsts.Count)];
                var last = lasts[random.Next(0, lasts.Count)];
                var streetNumberInt = random.Next(101, 999);
                var streetNumber = streetNumberInt.ToString();
                var zipCode = random.Next(10000, 99999).ToString();
                var phone = random.Next(200, 999) + "-555-"
                                + random.Next(2000, 9099);
                var fax = random.Next(200, 999) + "-555-"
                          + random.Next(2000, 9099);
                var email = first + "@"
                                + isp[random.Next(0, isp.Count)]
                                + ".com";

                               new Customer(
                                   streetNumber + " "
                                                       0, streets.Count - 1)
                                   + " Street",
                                          random.Next(0, cities.Count - 1)
                                          random.Next(0, states.Count - 1)

    public class Customer

        public string First { get; set; }
        public string Last { get; set; }
        public string Name { get { return First + " " + Last; } }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
        public string Email { get; set; }

        public Customer()
        { }

        public Customer(
            string first,
            string last,
            string address,
            string city,
            string state,
            string zip,
            string phone,
            string fax,
            string email)
            First = first;
            Last = last;
            Address = address;
            City = city;
            State = state;
            Zip = zip;
            Phone = Phone;
            Fax = fax;
            Email = email;


The VM consists of a single property of type PagedCollectionView (the type expected by the DataPager) and a constructor,

public class MainViewModel : ViewModelBase

    private PagedCollectionView dataListview;
    public PagedCollectionView DataListView
        get { return dataListview; }

    public MainViewModel()
        var customerCollection = new CustomerCollection();
        var customers = customerCollection.Customers;
        var customers2 = customers.OrderBy( c => c.Zip );
        dataListview = new PagedCollectionView( customers2 );

The constructor begins by instantiating a CustomerCollection and then using that to extract the Customers ObservableCollection.  We then introduce a new variable, customers2, which uses a Linq statement to order the results, and that is presented to the PageCollectionView constructor, and the result is stored in the backing member for the public property.

CAUTION:  customers2 is now of type IOrderedEnumerable, not ObservableCollection, and if you need an ObservableCollection you’ll need to convert it to be one.  For our purposes (read only) it serves just fine.

Setting the DataContext

The final link is to set the data context in MainPage.xaml.cs

void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
    DataContext = new MainViewModel().DataListView;

The result is a pageable datagrid of Customers.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in Community, Data, Patterns & Skills and tagged . Bookmark the permalink.

One Response to Great Silverlight Competency Test Part 4 (Answer)

Comments are closed.