Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / WPF

Empty groups in WPF ListView

Rate me:
Please Sign up or sign in to vote.
2.44/5 (5 votes)
8 Mar 2009CPOL 55K   1.7K   20   4
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Russian Federation Russian Federation
Sergey likes doing programming every day for the last 15 years started with Basic, ASM, Pascal, C++, X++ and ended with C# these days. User Interface design, usability, communications, parallel and asynchronous programming, game development and 3D graphics - all these areas makes him excited.

MCPD Windows, Web and Enterprise

Comments and Discussions

 
PraiseThank you Pin
DennisXWu22-Apr-16 13:49
DennisXWu22-Apr-16 13:49 
QuestionEmpty groups in WPF ListView Pin
GIdenJoe29-Aug-12 9:21
GIdenJoe29-Aug-12 9:21 
GeneralStore expander status Pin
evgeniikim22-Jul-09 7:07
evgeniikim22-Jul-09 7:07 
GeneralName Name Name Pin
Member 47495631-Jul-09 0:29
Member 47495631-Jul-09 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.