Click here to Skip to main content
12,063,645 members (71,470 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

31K views
1.3K downloads
18 bookmarked
Posted

Empty groups in WPF ListView

, 8 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Sergey Galich
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

You may also be interested in...

Comments and Discussions

 
QuestionEmpty groups in WPF ListView Pin
GIdenJoe29-Aug-12 10:21
memberGIdenJoe29-Aug-12 10:21 
GeneralStore expander status Pin
evgeniikim22-Jul-09 8:07
memberevgeniikim22-Jul-09 8:07 
GeneralName Name Name Pin
Member 47495631-Jul-09 1:29
memberMember 47495631-Jul-09 1: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.

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