Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Empty groups in WPF ListView

0.00/5 (No votes)
8 Mar 2009 1  
How to add empty groups to the WPF ListView control.

EmptyClusters.png

Introduction

WPF has a ListView control which uses the CollectionView class for sorting and grouping. MSDN gives an example of how to use this class to derive groups from the items list. However, there is no example of how to add empty groups, which can't be derived from the items list.

Consider an example scenario - the user must be presented with a servers list, grouped by clusters. The ListView can be used to display the servers (primary information) and grouped by clusters (secondary information). The user must be able to add a new cluster to populate it with servers. After a new cluster is added, it is not displayed because there is no server referencing the new cluster.

Using the code

You can add groups using the GroupDescription.GroupNames property.

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

namespace EmptyGroups
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            var clusters = new[]
            {
                new Cluster { Name = "Front end" },
                new Cluster { Name = "Middle end" },
                new Cluster { Name = "Back end" },
            };

            var collectionView = new ListCollectionView(new[]
            {
                new Server { Cluster = clusters[0], Name = "webshop1" },
                new Server { Cluster = clusters[0], Name = "webshop2" },
                new Server { Cluster = clusters[0], Name = "webshop3" },
                new Server { Cluster = clusters[0], Name = "webshop4" },
                new Server { Cluster = clusters[0], Name = "webshop5" },
                new Server { Cluster = clusters[0], Name = "webshop6" },
                new Server { Cluster = clusters[2], Name = "sql1" },
                new Server { Cluster = clusters[2], Name = "sql2" },
            });

            var groupDescription = new PropertyGroupDescription("Cluster.Name");

            // this foreach must at least add clusters that can't be
            // derived from items - i.e. groups with no items in them
            foreach (var cluster in clusters)
                groupDescription.GroupNames.Add(cluster.Name);

            collectionView.GroupDescriptions.Add(groupDescription);
            ServersList.ItemsSource = collectionView;
            Clusters = groupDescription.GroupNames;
        }

        readonly ObservableCollection<object> Clusters;

        void AddNewCluster_Click(object sender, RoutedEventArgs e)
        {
            Clusters.Add(NewClusterName.Text);
        }
    }

    class Cluster
    {
        public string Name { get; set; }
    }

    class Server
    {
        public Cluster Cluster { get; set; }
        public string Name { get; set; }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here