I have an XML file (see below) and can display all the Product Names in a listbox.
I want each entry in the listbox to display Product Name followed by Price, not just Product Name.
How do I do the datatemplate in the XAML file? Thanks.
Simplified XML file:
<Product>
<Name>Red Chair</Name>
<Price>29.5</Price>
</Product>
Simplified XAML file:
<DockPanel>
<ListBox Name="listBox1" ItemsSource="{Binding}" Margin="10" >
</ListBox>
</DockPanel>
In my C# file, I use LINQ to collect the products from the XML file and assign var products to listBox1.DataContext and it works fine. Now I just want to add in the Price. Thanks.
You do this the same as any other
ItemTemplate.Make sure that you’re binding to the
Product, not theName. You can then select the values from the XML using XPath, something like this.