[英]How to bind list inside ListView in Xamarin.Forms
I have one ListView on my page having ItemSource
as List<AssetModel>
as shown below:我的页面上有一个 ListView,其
ItemSource
为List<AssetModel>
,如下所示:
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; }
}
How can I bind TaskDetail
list in my parent list?如何在我的父列表中绑定
TaskDetail
列表?
Desired Layout:所需布局:
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 .
James Montemagno 写了一篇关于这种需求的文章,应该会对你有很大帮助。
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'.总之,分组功能需要一个“列表列表”类型的对象(
IEnumerable<IEnumerable<>>
),其中每个“主项目”是“详细项目”的列表。
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);
}
}
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>>();
This AssetsList
is what you should bind to the ItemsSource
of ListView
这个
AssetsList
是你应该绑定到ListView
的ItemsSource
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);
}
Then in your XAML you define your ListView Grouping properties:然后在 XAML 中定义 ListView 分组属性:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.