Xamarin 列表视图分组 [英] Xamarin Listview grouping
问题描述
我正在处理 xamarin.froms 中的列表视图.我可以轻松地为每条记录使用一个列表项填充列表视图:
<预><代码>[{猫":1,名称":阿尔法"},{猫":1,名称":测试版"},{猫":1,名称":伽玛"},{"cat":2, "name":"john"},{"cat":2, "name":"william"},{"cat":2, "name":"smith"},{"cat":2, "name":"steve"},{"cat":3, "name":"abc"},{猫":3,名称":xyz"}]//来自这个json源的listview中的9个项目
但我想要的是根据某个键值对所有项目进行分组,在这里说 "cat"
并实现如下所示:
对此的任何建议或方法将不胜感激.
将您的数据分组到集合中,并将这些集合添加到您的 ListView 中.集合需要是您自己的类,具有用于绑定组的属性.
这是一个演练:
在 ListView 上设置分组,包括将每个组绑定到的属性,在本例中为GroupKey"
myListView.IsGroupingEnabled = true;myListView.GroupDisplayBinding = new Binding("GroupKey");//见下文
然后添加您的数据分组(例如列表列表).这通常意味着您需要创建自己的类来显示您的分组,例如:
公共类分组:ObservableCollection{//注意:这是上面用于显示标题的 GroupDisplayBinding公共 K 组密钥 { 获取;私人订制;}公共分组(K 键,IEnumerable 项)ac{GroupKey = 密钥;foreach (var item in items)this.Items.Add(item);}}
最后,按组添加您的数据:
var groups = new ObservableCollection>();//你可以只传入一组数据(其中GroupA"是一个可枚举集)groups.Add(new Grouping("GroupA", GroupA));//或者过滤一组数据groups.Add(new Grouping("GroupB",MyItems.Where(a => a.SomeFilter())));myListView.ItemSource = 组;
像以前一样将您的单元格绑定到 MyDataClass:
var cell = new DataTemplate(typeof(TextCell));cell.SetBinding(TextCell.TextProperty, "SomePropertyFromMyDataClass");cell.SetBinding(TextCell.DetailProperty, "OtherPropertyFromMyDataClass");myListView.ItemTemplate = 单元格;
查看它以了解为什么使用模板 K
而不是 Grouping
类中的字符串、如何自定义标题外观等等:
http://motzcod.es/post/94643411707/enhancing-xamarinforms-带分组的列表视图
(归功于@pnavk 回答中的链接)
I am dealing with a listview in xamarin.froms. I can easily populate the listview with a listitem for each record:
[
{"cat":1, "name":"alpha"},
{"cat":1, "name":"beta"},
{"cat":1, "name":"gamma"},
{"cat":2, "name":"john"},
{"cat":2, "name":"william"},
{"cat":2, "name":"smith"},
{"cat":2, "name":"steve"},
{"cat":3, "name":"abc"},
{"cat":3, "name":"xyz"}
]
//9 Items in listview from this json source
But what I want is to group all the items on some key value, say "cat"
here and achieve something like this:
Any suggestion or approach toward this would be appreciated.
Group your data into collections and add those collections into your ListView. The collections need to be your own class with a property for binding the group with.
Here's a walkthrough:
Set up grouping on your ListView including a property to bind each group to, in this case "GroupKey"
myListView.IsGroupingEnabled = true;
myListView.GroupDisplayBinding = new Binding("GroupKey"); // See below
And then add your data in groups (e.g. lists of lists). This often means you need to create your own class to show your groupings, such as:
public class Grouping<K, T> : ObservableCollection<T>
{
// NB: This is the GroupDisplayBinding above for displaying the header
public K GroupKey { get; private set; }
public Grouping(K key, IEnumerable<T> items)ac
{
GroupKey = key;
foreach (var item in items)
this.Items.Add(item);
}
}
And finally, add your data in 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 the MyDataClass as you would have before:
var cell = new DataTemplate(typeof(TextCell));
cell.SetBinding(TextCell.TextProperty, "SomePropertyFromMyDataClass");
cell.SetBinding(TextCell.DetailProperty, "OtherPropertyFromMyDataClass");
myListView.ItemTemplate = cell;
Check it out for explanation on why to use template K
instead of a string in the Grouping
class, how to customise the header look, and much more:
http://motzcod.es/post/94643411707/enhancing-xamarinforms-listview-with-grouping
(Credit to the link in @pnavk's answer)
这篇关于Xamarin 列表视图分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!