Apr 24


WPF ListView Grouping


Another feature that we have with the ListView is grouping. This will allow you to group rows depending on a specific field. This feature can be customized as needed and give your application extreme usefulness visualizing information.

Below you can see a sample with the XAML and the C# code:


namespace WpfApplication2
    /// Interaction logic for MainWindow.xaml
    public partial class MainWindow : Window
        public MainWindow()

            List businesses = new List();
            businesses.Add(new Business("Microsoft", "http://www.microsoft.com", "123-421-1231","Enterprise"));
            businesses.Add(new Business("SkyXoft", "http://www.skyxoft.com","123-321-1231","SMB"));
            businesses.Add(new Business("LicenseSpot", "http://www.licensespot.com", "123-312-3212","SMB"));

            BusinessListView.ItemsSource = businesses;

            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(BusinessListView.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Size");

    public class Business
        public string Company { get; set; }
        public string Url { get; set; }
        public string Phone { get; set; }
        public string Size { get; set; }
        public Business(string company, string url, string phone, string size)
            this.Company = company;
            this.Url = url;
            this.Phone = phone;
            this.Size = size;

In this sample we've taken the list of businesses and grouping them based on the size of the company. In the XAML we've just added a GroupStyle to the list view in which a template is defined. This template includes a TextBlock that holds the information for the grouping. It has a different style, a bold, to note the difference. The text property is bound to the Name field but it has nothing to do with the Business object. This Name property holds the name of the group as assigned by WPF.

In the C# code we're just adding the objects to a List but we're also using the CollectionView object which has the ability to group items. We do so by creating a PropertyGroupDescription object and adding it as a GroupDescription in the CollectionView. This tells WPF to group the items using the specified property.

Add an expander to the group header

In the previous example, we showed how to group the rows in the ListView. But one common request from end users is to have the ability to expand and collapse the group headers so they can filter the information they see in the screen. In WPF in can't do this by default but we can some templates to do it.

Below you can find the XAML code:



There's no need to modify the C# code, we can use the same one. In the XAML code we're just adding the Expander property and setting it to true to show it the ListView. As you can see in the screenshot, we can now group by the size of the company. We also added the number of items inside the group using the ItemCount property which is also available in the group.

With this, you can quickly style the group to your own requirements and build grid interfaces that really provides added value to your users.

No Comments

Add Comment