我有ListView
绑定到ObservableCollection<Request>
。这是Request
类:
public class Request
{
public int Id { get; set; }
public string RequestName { get; set; }
public string Provider { get; set; }
public int Done { get; set; }
}
我在Done
中只有2个可能的值:0
或1
。 Request
Done
将0
设置为dict
一组,其他dict
设置为其他组。怎么可能这样做?
答案 0 :(得分:4)
以下是如何对其进行分组。您可能需要进一步加强这一点,以使其更加用户友好。
分组属性
private ObservableCollection<Grouping<String, Request>> _groupedList = null;
public ObservableCollection<Grouping<String, Request>> GroupedList {
get {
return _groupedList;
}
set {
_groupedList = value;
RaisePropertyChanged(() => GroupedList);
}
}
创建列表
var list = new List<Request>();
var grouped = from Model in list
group Model by Model.Done into Group
select new Grouping<string, Request>(Group.Key, Group);
GroupedList = new ObservableCollection<Grouping<string, TModel>>(grouped);
ListView Xaml
<ListView IsGroupingEnabled="true" HasUnevenRows="true">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Key}" VerticalOptions="Center"/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<ContentView Padding="10">
<Label Text={Binding RequestName}" />
</ContentView>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>