Xamarin表单ListView分组绑定问题

我一直试图玩Xamarin Forms一段时间,然后遇到了Listview Grouping。 我没有完成显示空白列表。 请帮我找一下我错的地方? 提前致谢

但是,我的类域看起来像:

public class LineItemTaxDto { public int InvoiceLineItemId { get; set; } public int InvoiceId { get; set; } public int TaxId { get; set; } public decimal TaxRate { get; set; } public decimal TaxAmount { get; set; } public string TaxName { get; set; } } 

我的视图模型属性看起来像

  public IEnumerable<KeyValuePair<string, ObservableCollection>> ReceiptTaxList { get; set; } 

我期待的结果如下:

在此处输入图像描述

我的xaml代码如下

       

编辑

我在视图中设置值会出现方法

 Invoice.ReceiptTaxList = Invoice.InvoiceLineItems.Select(x => { return new KeyValuePair<string, ObservableCollection>(x.TaxName, x.LineItemTaxes); }); 

该值设置正确

我已经解决了这个棘手的代码,不使用分组,但现在它的工作正如预期的那样

我的ViewModel代码

 var TaxList = Invoice.InvoiceLineItems.Where(x => x.TaxId > 1).GroupBy(y=>y.TaxId > 1).Select(x => { return new KeyValuePair>(new LineItemTaxDto() { InvoiceLineItemId = x.First().Id, InvoiceId = x.First().InvoiceId, TaxId = x.First().TaxId, TaxRate = x.First().TaxRate, TaxAmount = x.Sum(a=>a.TaxAmount), TaxName = taxlabel + " (" + x.First().TaxName + ")" }, x.First().LineItemTaxes); }); var taxes = new ObservableCollection(); foreach (var tax in TaxList.GroupBy(tax => tax.Key.TaxId).Select(grp => grp.First())) { taxes.Add(tax.Key); foreach (var subtax in tax.Value.Where(x => x.TaxId > 0)) { taxes.Add(subtax); } } Invoice.ReceiptTaxList = taxes; 

我的Xaml代码

                   

(抱歉可怜的英文)

您必须使用列表列表作为listview上的项目源才能使其正常工作。 我建议你创建一个具有你想要在头部显示的属性的类,如下所示:

 public class TaxesObservableCollection : ObservableCollection // Think about to implement INotifyPropertyChanged too, I guess it will be usefull { public TaxesObservableCollection(IEnumerable collection, string taxGroupName) : base(collection) { TaxGroupName = taxGroupName; } private bool taxGroupName; public bool TaxGroupName { get { return taxGroupName; } set { taxGroupName = value; } } } 

在您的视图模型中:

 public IEnumerable> ReceiptTaxList { get; set; } 

你的ListView:

 (...) // The rest of it as you did        (...) // The rest of it as you did 

你这样填写:

 Invoice.ReceiptTaxList = Invoice.InvoiceLineItems.Select(x => { return new TaxesObservableCollection(x.LineItemTaxes, x.TaxName); }).ToList(); 

它应该有效,请让我知道你想要的是什么。