Click here to Skip to main content
Licence CPOL
First Posted 8 Mar 2009
Views 17,438
Downloads 454
Bookmarked 14 times

Empty groups in WPF ListView

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

About the Author

Sergey Galich

Software Developer (Senior)

Russian Federation Russian Federation

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralStore expander status Pinmemberevgeniikim7:07 22 Jul '09  
GeneralName Name Name PinmemberMember 47495630:29 1 Jul '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 8 Mar 2009
Article Copyright 2009 by Sergey Galich
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid