简体   繁体   中英

Bind ListView with XML (WPF) Page

I have window with Frame element in Center.Have a Page which contain all XML elements but I can`t realize it in ListBox.I attach example with using how it should look.I found many source code here but all they looks simple or not XML bind. Screenshot with description

I can put here Grid code if it possbile to make dynamic add elements. Sorry for my English. XML

<?xml version="1.0" encoding="utf-8"?>
<Cars>
  <Car>
    <Firm>Toyota</Firm>
    <Image>cruzer.jpg</Image>
    <Model>Land Cruiser</Model>
    <Year>2017</Year>
  </Car>
  <Car>
    <Firm>Toyota</Firm>
    <Image>cruzer.jpg</Image>
    <Model>Land Cruiser</Model>
    <Year>2017</Year>
  </Car>
</Cars>

You need to read in the XML and convert it into an internal model that can be consumed by an ObservableCollection. Reading Xml into a C# object model is for example explained here:

https://www.codeproject.com/Articles/1222133/Reading-and-Writing-XML-in-Csharp-VB-Net

Your case is probably such that you want to use a list structure instead of a tree structure as explained in the linked article. But reading the XML into an internal list structure like:

ObservableCollection<Car> ListCars { get; }

where Car is a simple object structure like:

public class Car
{
  public string Firm  { get; set; }
  public string  Pic  { get; set; }
  public string Model { get; set; }
  public string Year  { get; set; }
}

Is something you could learn from this or other articles on reading XML. The when you have the list you could bind it to a listbox.

Please be more specific about your problem if this is not helpful. Show some code of your attempt so far and explain why you can't get the display in a Listbox?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM