2017-08-09 127 views
1

我一直在尝试围绕Xamarin表单玩一段时间,然后碰到Listview分组。我没有完成它的显示空白列表。请给我帮忙找到我要去的地方错了吗?在此先感谢Xamarin表单ListView分组绑定问题

然而,我的班级域看起来像:

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<LineItemTaxDto>>> ReceiptTaxList { get; set; } 

我希望结果是这样的:

enter image description here

我的xaml代码低于

  <ListView x:Name="TaxListView" 
       Grid.Row="2" 
       Grid.Column="0" 
       Grid.ColumnSpan="2" 
       ItemsSource="{Binding Invoice.ReceiptTaxList}" 
       IsGroupingEnabled="true"> 
       <ListView.GroupHeaderTemplate> 
        <DataTemplate> 
         <ViewCell> 
          <Label Text="{Binding Key}" /> 
         </ViewCell> 
        </DataTemplate> 
       </ListView.GroupHeaderTemplate> 

       <ListView.ItemTemplate> 
        <DataTemplate> 
         <ViewCell> 
          <Grid RowSpacing="0" ColumnSpacing="0"> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="Auto" /> 
           </Grid.RowDefinitions> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="*" /> 
            <ColumnDefinition Width="150" /> 
           </Grid.ColumnDefinitions> 

           <Label Text="{Binding TaxName}" Grid.Row="0" Grid.Column="0" FontSize="22" FontFamily="{x:Static resources:Fonts.ArialMTFont}" HorizontalOptions="End" HorizontalTextAlignment="End" Margin="0,5,0,5" /> 

           <Label Grid.Row="0" Grid.Column="1" Text="{Binding TaxAmount, Converter={Helpers:CurrencyAmountConverter}}" FontSize="22" FontFamily="{x:Static resources:Fonts.ArialMTFont}" HorizontalOptions="End" Margin="0,5,0,5" /> 
          </Grid> 
         </ViewCell> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 

编辑

我设定值视图将出现方法

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

的值设置不当

+0

什么s是'重要'财产?你如何填补它?你能发布代码吗?也许我可以帮你 –

+0

请再次检查我的问题我编辑它。 –

回答

0

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

我的视图模型代码

var TaxList = Invoice.InvoiceLineItems.Where(x => x.TaxId > 1).GroupBy(y=>y.TaxId > 1).Select(x => 
{ 
    return new KeyValuePair<LineItemTaxDto, ObservableCollection<LineItemTaxDto>>(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<LineItemTaxDto>(); 
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 
x:Name="TaxListView" 
ItemsSource="{Binding Invoice.ReceiptTaxList}" 
Grid.Row="2" 
Grid.Column="0" 
Grid.ColumnSpan="2" 
VerticalOptions="FillAndExpand" 
HorizontalOptions="FillAndExpand" 
SeparatorVisibility="None" 
HasUnevenRows="false" 
RowHeight="35" 
> 
<ListView.ItemTemplate> 
    <DataTemplate> 
     <ViewCell> 
      <Grid RowSpacing="0" ColumnSpacing="0"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="150" /> 
       </Grid.ColumnDefinitions> 
       <Label Grid.Row="0" Grid.Column="0" Text="{Binding TaxName}" FontSize="22" FontFamily="{x:Static resources:Fonts.ArialMTFont}" HorizontalOptions="End" HorizontalTextAlignment="End" Margin="0,5,0,5" /> 
       <Label Grid.Row="0" Grid.Column="1" Text="{Binding TaxAmount, Converter={Helpers:CurrencyAmountConverter}}" FontSize="22" FontFamily="{x:Static resources:Fonts.ArialMTFont}" HorizontalOptions="End" Margin="0,5,0,5" /> 
      </Grid> 
     </ViewCell> 
    </DataTemplate> 
</ListView.ItemTemplate> 

-1

(对不起,我英文不好)

你必须在列表视图上使用列表作为项目来源才能使其工作。我建议你创建你想在这样的标题来显示属性的类:

public class TaxesObservableCollection<T> : ObservableCollection<T> // Think about to implement INotifyPropertyChanged too, I guess it will be usefull 
{ 
    public TaxesObservableCollection(IEnumerable<T> collection, string taxGroupName) : base(collection) 
    { 
     TaxGroupName = taxGroupName; 
    } 

    private bool taxGroupName; 
    public bool TaxGroupName 
    { 
     get { return taxGroupName; } 
     set { taxGroupName = value; } 
    } 
} 

在您的视图模型:

public IEnumerable<TaxesObservableCollection<LineItemTaxDto>> ReceiptTaxList { get; set; } 

你的ListView:

(...) // The rest of it as you did 
<ListView.GroupHeaderTemplate> 
    <DataTemplate> 
     <ViewCell> 
      <Label Text="{Binding TaxGroupName}" /> 
     </ViewCell> 
    </DataTemplate> 
</ListView.GroupHeaderTemplate> 
(...) // The rest of it as you did 

你用这种方式填充:

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

它应该工作,请让我知道,如果是你想要的。