Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

GroupedObservableCollection

Mark Smith edited this page Aug 29, 2016 · 1 revision

GroupedObservableCollection

The GroupedObservableCollection<TKey,TValue> is an observable collection of TValue items which supports grouping. You can use this collection as the direct data source for a ListView with grouping turned on.

Properties

  • Key : the read-only value the items are grouped by.
  • HasItems : true/false whether the collection has any items

Remarks

The key must be passed to one of the two constructors and is considered a read-only value.

Example

In this code sample, we take a set of un-ordered items, group them by the first letter and then turn that into an ObservableCollection. Our final GroupedPeople will be of type ObservableCollection<GroupedObservableCollection<string,Person>>.

using XamarinUniversity.Infrastructure;
using using XamarinUniversity.Collections;
...
 
public class MyViewModel : SimpleViewModel
{
   public IList<GroupedObservableCollection<string,Person>> GroupedPeople { get; set; }

   public MyViewModel()
   {
       var allPeople = GetPeople(); // un-grouped list.
       GroupedPeople = allPeople.GroupBy(p => p.Name[0].ToString(), p => p)
                           .ToGroupedObservable ()    // IEnumerable<GroupedObservable..>
                           .ToObservableCollection(); // ObservableCollection<GroupedObservable..>>
   }
}