Search code examples
c#wpflistviewwindows-store-appsgrouping

ListView Grouping?


I have a Collection that I want to group. The grouping is like below.

Let's say I have a Person collection. Person has properties Name, Country, Gender.

What I wan to do in my ListView is group by country first, then group by gender, and the show the name of the person. For example, I want to see list of Names that is Male under USA, and another for other countries as well.

Also, when I add to this collection dynamically, ex. A new person, the grouping should still take effect, ie, he/she appears in the proper country / gender.

Lastly, I also need that the Grouping can be selected.. For example, I can click on the USA group, and then click on the 'Male' group and enter a new person there.

How do I achieve this?

Thank you!


Solution

  • MSDN has a full example on how to group your collection. Then display it to a LiewView or GridView.

    How to group items in a list or grid (XAML)


    If you want to add an item after the grouping, then you will probably have to re-run the grouping code again -- or you can add it to the sub-collection with the key value that you're matching.


    As for your "Grouping can be selected" that is entirely up to you. Take the look at the example above, and modify the LINQ query to fit your needs.