Xamarin Forms ListView分组绑定问题 [英] Xamarin Forms ListView grouping bind issue

查看:137
本文介绍了Xamarin Forms ListView分组绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试使用Xamarin Forms,然后遇到Listview Grouping.我还没有完成其显示空白列表.请给我帮助以查找我要去哪里哪里?预先感谢

I have been trying to play around Xamarin Forms for a while and then came across to the Listview Grouping. I am not getting done its showing blank list. Please give me help to find where I am going wrong? Thanks in advance

但是,我的课程域看起来像:

However, My class domain looks like:

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

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

My View Model property look like

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

我的预期结果如下:

下面的我的xaml代码

My xaml code below

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

编辑

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

I am set value in view will appear method

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

该值设置正确

推荐答案

我已经解决了这个棘手的代码,它没有使用分组功能,但是现在可以按预期工作

I have solved this tricky code not using grouping but now its work as expected

我的ViewModel代码

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>

这篇关于Xamarin Forms ListView分组绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆