如何在Xamarin.Forms中的ListView内绑定列表 [英] How to bind list inside ListView in Xamarin.Forms

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

问题描述

我的页面上有一个ListView,其ItemSource作为List<AssetModel>,如下所示:

I have one ListView on my page having ItemSource as List<AssetModel> as shown below:

public class AssetModel
{
    public string AssetId { get; set; }
    public string Description { get; set; }

    public List<TaskDetail> TaskDetailList { get; set; }
}

public class TaskDetail
{
    public string Description { get; set; }
}

如何在父列表中绑定TaskDetail列表?

How can I bind TaskDetail list in my parent list?

所需的版式:

推荐答案

似乎是经典的分组listview用例. 詹姆斯·蒙特马格诺(James Montemagno)撰写了一篇有关这种需求的文章,该文章应该会对您有很大帮助.

It seems like a classic grouping listview use case. James Montemagno wrote an article about this kind of need that should help you a lot.

总而言之,分组功能需要一个类型为列表列表"(IEnumerable<IEnumerable<>>)的对象,其中每个主项目"都是详细项目"的列表.

In summary, the grouping feature expects an object of type 'List of List' (IEnumerable<IEnumerable<>>), where each 'master item' is a list of 'detail item'.

为简便起见,您可以使用上述文章中提供的类:

To make it easy, you can use the class provided at the above mentioned article:

public class Grouping<K, T> : ObservableCollection<T>
{
    public K Key { get; private set; }

    public Grouping(K key, IEnumerable<T> items)
    {
        Key = key;
        foreach (var item in items)
            this.Items.Add(item);
    }
}

然后,您必须将list属性的类型更改为例如:

Then, the list property you must change its type to, for example, this:

ObservableCollection<Grouping<AssetModel, TaskDetail>> AssetsList { get; set; } = 
    new ObservableCollection<Grouping<AssetModel, TaskDetail>>();

AssetsList是您应绑定到ListViewItemsSource

This AssetsList is what you should bind to the ItemsSource of ListView

要填充此属性,例如,您需要执行以下操作:

To fill this property, you'll need, for example, do this:

for (int i = 0; i < 5; i++)
{
    var asset = new AssetModel();
    asset.AssetId = new Guid().ToString();
    asset.Description = $"Asset { i + 1} ";
    asset.TaskDetailList = new List<TaskDetail>();

    for (int j = 0; j < 3; j++)
        asset.TaskDetailList.Add(new TaskDetail() { Description = $"Detail { (i + 1) } - { (j + 1) }" });

    var group = new Grouping<AssetModel, TaskDetail>(asset, asset.TaskDetailList);

    AssetsList.Add(group);
}

然后在XAML中定义ListView分组属性:

Then in your XAML you define your ListView Grouping properties:

<ListView ItemsSource="{Binding AssetsList}" 
          HasUnevenRows="True" 
          SeparatorVisibility="None"
          SeparatorColor="Transparent"
          IsGroupingEnabled="True">
    <ListView.GroupHeaderTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="AssetId"
                               FontAttributes="Bold"/>
                        <Label Text={Binding Key.AssetId}/>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="Description"
                               FontAttributes="Bold"/>
                        <Label Text={Binding Key.Description}/>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.GroupHeaderTemplate>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text={Binding Description}/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这篇关于如何在Xamarin.Forms中的ListView内绑定列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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