Xamarin表单:分组ObservableCollection

时间:2015-12-04 14:18:58

标签: c# listview binding xamarin xamarin.forms

我有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个可能的值:01Request Done0设置为dict一组,其他dict设置为其他组。怎么可能这样做?

1 个答案:

答案 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>