显示 CollectionView 分组在 Xamarin 中不起作用

Show CollectionView Grouping not working in Xamarin

我有一篇文章 about showing Data Groups from Preferences . As per everyone's input I switched to CollectionView. I have consulted the article https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/grouping。这是我的:

public class CartUser
{       
    public int IDProduct { get; set; }
    public string NameProduct { get; set; } 
    public string SupplierID { get; set; }
}

SupplierIDGrouping.cs

public class SupplierIDGrouping : ObservableCollection<CartUser>
{
    public string SupplierID { get; private set; }

    public SupplierIDGrouping(string supplierID)
    : base()
    {
        SupplierID = supplierID;
    }

    public SupplierIDGrouping(string supplierID, IEnumerable<CartUser> source)
        : base(source)
    {
        SupplierID = supplierID;
    }

}

PageOne.xaml

<CollectionView ItemsSource="{Binding SupplierList}" IsGrouped="true">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Label Text="{Binding NameProduct}"/>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

PageOne.xaml.cs

public ObservableCollection<SupplierIDGrouping> SupplierList { get; private set; } = new ObservableCollection<SupplierIDGrouping>();

List<CartUser> cartUsers = new List<CartUser>();
var mycart = Preferences.Get("CartUserAdds", "_mycart");
var getcart = JsonConvert.DeserializeObject<List<CartUser>>(mycart);
cartUsers = getcart;

foreach (var item in cartUsers)
{
    if (!SupplierList.Any(supplierid => supplierid.SupplierID == item.SupplierID))
    {
        SupplierList.Add(new SupplierIDGrouping(item.SupplierID));
    }

    SupplierList.Single(supplierid => supplierid.SupplierID== item.SupplierID).Add(item);
}

BindingContext = this;

我取自Preferences的数据:

[{\"IDProduct\":1,\"NameProduct\":\"Name product 1\",\"SupplierID\":\"22379356\"},{\"IDProduct\":2,\"NameProduct\":\"Name product 2\",\"SupplierID\":\"22379356\"},{\"IDProduct\":3,\"NameProduct\":\"Name product 3\",\"SupplierID\":\"12336544\"}]

但是我的结果仍然没有按 SupplierID

分组

这就是我想要的:

期待大家的帮助。非常感谢!

更新

数据对应2组。第 1 组:2 个产品,第 2 组:1 个产品

foreach (var item in cartUsers)
{
    if (!SupplierList.Any(supplierid => supplierid.SupplierID == item.SupplierID))
    {
        SupplierList.Add(new SupplierIDGrouping(item.SupplierID));
    }

    SupplierList.Single(supplierid => supplierid.SupplierID== item.SupplierID).Add(item);
}

var getresult = SupplierList;
foreach(var i in getresult)
{

}

BindingContext = this;

更新 2

public class SupplierIDGrouping : ObservableCollection<CartUser>
{
    public string SupplierID { get; private set; }
    public string Name { get { return SupplierID; } }

    public SupplierIDGrouping(string supplierID)
    : base()
    {
        SupplierID = supplierID;
    }

    public SupplierIDGrouping(string supplierID, IEnumerable<CartUser> source)
        : base(source)
    {
        SupplierID = supplierID;
    }    
}

PageOne.xaml

<CollectionView ItemsSource="{Binding SupplierList}" IsGrouped="true" Header="{Binding Name}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Label Text="{Binding NameProduct}"/>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

我必须提供 GroupHeaderTemplate 才能使其正常工作。不知道为什么 - 根据文档,应该没有必要

    <CollectionView.GroupHeaderTemplate>
        <DataTemplate>
            <Label Text="{Binding SupplierID}"
               BackgroundColor="LightGray"
               FontSize="Large"
               FontAttributes="Bold" />
        </DataTemplate>
    </CollectionView.GroupHeaderTemplate>