【问题标题】:Xamarin Forms ListView grouping bind issueXamarin Forms ListView 分组绑定问题
【发布时间】:2018-01-17 11:36:23
【问题描述】:

我一直在尝试使用 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<LineItemTaxDto>>> ReceiptTaxList { get; set; }

我的预期结果如下:

下面是我的 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); });

值设置正确

【问题讨论】:

  • 什么是'Key'属性?以及如何填写?你可以发布代码吗?也许我可以帮助你
  • 请再次检查我的问题,我编辑了它。

标签: c# listview xamarin.forms grouping


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您必须使用列表列表作为列表视图上的项目源才能使其正常工作。我建议您使用要在标题中显示的属性创建一个类,如下所示:

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

    您的列表视图:

    (...) // 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();
    

    它应该可以工作,如果是你想要的,请告诉我。

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 2019-02-15
      • 1970-01-01
      相关资源
      最近更新 更多