Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay master detail data in nested ListBox
    primarykey
    data
    text
    <p>I want to display my data like this</p> <p><img src="https://i.stack.imgur.com/DyU6j.png" alt="enter image description here"></p> <p>Here is the class definition:</p> <pre><code>public class Order { public string Customer { get; set; } public decimal Amount { get; set; } public List&lt;OrderDetail&gt; Details = new List&lt;OrderDetail&gt;(); } public class OrderDetail { public string Product { get; set; } public int Qty { get; set; } } </code></pre> <p>I try to use ListBox but fail to display Details. Below is my try in xaml:</p> <pre><code>&lt;ListBox x:Name="lstOrder"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Customer}"/&gt; &lt;TextBlock Text="{Binding Amount}"/&gt; &lt;ListBox ItemsSource="{Binding Details}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Product}"/&gt; &lt;TextBlock Text="{Binding Qty}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>in code behind:</p> <pre><code>public MainWindow() { InitializeComponent(); List&lt;Order&gt; OrderList = new List&lt;Order&gt;(); 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; } </code></pre> <p>What is the correct way to bind OrderList.Details? Any suggestions will be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload