问题描述
我正在制作一个移动应用程序,并且试图在列表视图中加载列表。该列表包含多个元素和一个列表:
public static List<Proposition> PropositionList = new List<Proposition>
{
new Proposition{
PropositionId = Guid.Parse("00000000-0000-0000-0000-000000000001"),Place= "Barge",Date= new DateTime(2020,7,11),Users= new List<User>(){
new User
{
UserId = Guid.Parse("00000000-0000-0000-0000-000000000001"),Name= "Jan Aerts",Sterren = 3
},new User
{
UserId = Guid.Parse("00000000-0000-0000-0000-000000000002"),Naam = "Bart Denys",Sterren = 5
}
}
}
}
如何使用x:Name =“ StacklayoutMap”在堆栈布局中从Users.Name列出列表
<ListView x:Name="lvPropositions" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BorderColor="Black" Margin="10,10,0" CornerRadius="45">
<StackLayout StyleClass="listitem" Padding="5">
<StackLayout Orientation="Horizontal">
<Label Style="{DynamicResource divesLabelStyle}" Text="{Binding Datum}" VerticalOptions="Center" HorizontalOptions="StartAndExpand" />
<Label Style="{DynamicResource divesLabelStyle}" Text="{Binding Plaats}" VerticalOptions="Center" HorizontalOptions="EndAndExpand" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<StackLayout x:Name="StackLayoutMap">
</StackLayout>
<ImageButton Source="addBlack.png" BackgroundColor="Transparent" HorizontalOptions="EndAndExpand" VerticalOptions="Center" Clicked="BtnRegister_Clicked"/>
</StackLayout>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
解决方法
您可以使用可绑定版式
在Xaml中
<StackLayout Orientation="Horizontal">
<StackLayout x:Name="StackLayoutMap" BindableLayout.ItemsSource="{Binding Users}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Naam}" TextColor="Red" ... />
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
//...
</StackLayout>
有关可绑定布局的更多详细信息,您可以参考此 doc 。