You need to enable grouping as follows:
myListView.IsGroupingEnabled = true; myListView.GroupDisplayBinding = new Binding("GroupKey");
Then add your data to groups (for example, lists of lists). This often means that you need to create your own class to show your groups, for example:
public class Grouping<K, T> : ObservableCollection<T> {
Finally, add your data to the groups:
var groups = new ObservableCollection<Grouping<string, MyDataClass>>(); // You can just pass a set of data in (where "GroupA" is an enumerable set) groups.Add(new Grouping<string, MyDataClass>("GroupA", GroupA)); // Or filter down a set of data groups.Add(new Grouping<string, MyDataClass>("GroupB", MyItems.Where(a => a.SomeFilter()))); myListView.ItemSource = groups;
Bind your cell to MyDataClass, as before:
var cell = new DataTemplate(typeof(TextCell)); cell.SetBinding(TextCell.TextProperty, "SomePropertyFromMyDataClass"); cell.SetBinding(TextCell.DetailProperty, "OtherPropertyFromMyDataClass"); myListView.ItemTemplate = cell;
Credit for the link in @pnavk's answer.
Check out the explanation for using the K template instead of a string in the Grouping class, how to customize the appearance of the header, and more:
http://motzcod.es/post/94643411707/enhancing-xamarinforms-listview-with-grouping
source share