简体   繁体   English

Xamarin表单:分组ObservableCollection

[英]Xamarin Forms: Grouping ObservableCollection

I have a ListView binded to ObservableCollection<Request> . 我有一个ListView绑定到ObservableCollection<Request> Here's Request class: 这是Request类:

public class Request
{
    public int Id { get; set; }
    public string RequestName { get; set; }
    public string Provider { get; set; }
    public int Done { get; set; }
}

I have only 2 possible values in Done : 0 or 1 . 我在Done只有两个可能的值: 01 Request s that have Done set to 0 to one group, others to other group. RequestDone设置为0到一个组,其他Request设置为其他组。 How is it possible to do this? 怎么可能这样做?

Here is how you would group it. 这是你如何组合它。 You may need to enhance on this further to make it a bit more user friendly. 您可能需要进一步加强这一点,使其更加用户友好。

Grouped Property 分组财产

private ObservableCollection<Grouping<String, Request>> _groupedList = null;
public ObservableCollection<Grouping<String, Request>> GroupedList {
    get {
        return _groupedList;
    }
    set {
        _groupedList = value;
        RaisePropertyChanged(() => GroupedList);
    }
}

Creating List 创建列表

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);

The ListView Xaml 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按Xamarin形式的属性对对象的ObservableCollection进行排序 - Sorting an ObservableCollection of Objects by Properties in Xamarin Forms 根据日期Xamarin表单过滤ObservableCollection - Filter ObservableCollection based on date Xamarin Forms Xamarin表单:按日期分组列表视图 - Xamarin forms: Grouping a listview by date Xamarin Forms ListView分组绑定问题 - Xamarin Forms ListView grouping bind issue 更新ObservableCollection无法正确更新Xamarin Forms中的ListView - Updating ObservableCollection does not properly update ListView in Xamarin Forms 更新ObservableCollection之后,Xamarin.Forms ListView发送空事件 - After update of ObservableCollection Xamarin.Forms ListView sends null events 从ObservableCollection中删除项目 <Model> Xamarin表单中的列表 - Removing item from ObservableCollection<Model> list in Xamarin Forms Xamarin.forms 如何从 observablecollection 中获取属性并将其放入 CarouselView - Xamarin forms how to take the property from a observablecollection and put it into a CarouselView 将字典中的JSON API解析为Xamarin.Forms中的ListView的ObservableCollection - Parse JSON API from Dictionary to ObservableCollection for ListView in Xamarin.Forms C#Xamarin使用ObservableCollection在TabbedPage中形成嵌套Listiews - C# Xamarin forms Nested Listiews in TabbedPage with ObservableCollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM