我是新来的,也是 .Net Maui 开发人员。我使用 API 并通过 GET 请求接收要处理的数据。我收到的数据由包含不同数量的项目位置的收据组成。这些项目位置存储在一个数组中。可以动态显示这个数组吗?
无索引绑定
<Label Text="{Binding ReceiptPosition[].ModelNumber}" Grid.Column="5" Grid.Row="0" Margin="5,0,0,0"/>
<Label Text="{Binding ReceiptPosition[].ModelDescription}" Grid.Column="6" Grid.Row="0" Margin="5,0,0,0"/>
<Label Text="{Binding ReceiptPosition[].Quantity}" Grid.Column="7" Grid.Row="0" Margin="5,0,0,0"/>
<Label Text="{Binding ReceiptPosition[].SellingPrice}" Grid.Column="7" Grid.Row="0" Margin="5,0,0,0"/>
我的视图模型
internal partial class BelegPageViewModel : ObservableObject
{
[ObservableProperty]
ObservableCollection<BelegStruktur> belegeCollection = new();
[ObservableProperty]
private BelegStruktur beleg;
}
我的结构
internal class BelegStruktur
{
public DateTime? Date { get; set; }
public int StoreNumber { get; set; }
public int PointOfSaleNumber { get; set; }
public int ReceiptNumber { get; set; }
public ReceiptPosition[]? ReceiptPosition { get; set; }
}
public class ReceiptPosition
{
public string ModelNumber { get; set; }
public string ModelDescription { get; set; }
public float Quantity { get; set; }
public float SellingPrice { get; set; }
}
我已经通过在另一个 CollectionView 中使用 CollectionView 解决了这个问题。
示例:
<CollectionView ItemsSource="{Binding BelegeCollection}">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="{x:Type models:BelegStruktur}">
<StackLayout>
<Label Text="{Binding Date}" />
<Label Text="{Binding StoreNumber}" />
<Label Text="{Binding PointOfSaleNumber}" />
<Label Text="{Binding ReceiptNumber}" />
<CollectionView ItemsSource="{Binding ReceiptPosition}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="{x:Type models:ReceiptPosition}">
<StackLayout>
<Label Text="{Binding ModelNumber}" />
<Label Text="{Binding ModelDescription}" />
<Label Text="{Binding Quantity}" />
<Label Text="{Binding SellingPrice}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>