我想这样显示我的数据
这是类定义:
public class Order
{
public string Customer { get; set; }
public decimal Amount { get; set; }
public List<OrderDetail> Details = new List<OrderDetail>();
}
public class OrderDetail
{
public string Product { get; set; }
public int Qty { get; set; }
}
我尝试使用 ListBox 但无法显示详细信息。下面是我在 xaml 中的尝试:
<ListBox x:Name="lstOrder">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Customer}"/>
<TextBlock Text="{Binding Amount}"/>
<ListBox ItemsSource="{Binding Details}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Product}"/>
<TextBlock Text="{Binding Qty}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在后面的代码中:
public MainWindow()
{
InitializeComponent();
List<Order> OrderList = new List<Order>();
Order order = new Order { Customer = "David", Amount = 2000 };
order.Details.Add(new OrderDetail { Product = "A", Qty = 5 });
OrderList.Add(order);
order = new Order { Customer = "John", Amount = 5000 };
order.Details.Add(new OrderDetail { Product = "A", Qty = 2 });
order.Details.Add(new OrderDetail { Product = "B", Qty = 3 });
OrderList.Add(order);
lstOrder.ItemsSource = OrderList;
}
绑定 OrderList.Details 的正确方法是什么?任何建议将不胜感激。