In WPF / Silverlight: is it possible to link DataTable or XML file to ListView?

Is it possible in WPF to link a data matrix (data table or XML file) to a ListView?

Given the following XML dataset:

<data>
  <cols>
    <col name="FirstName" />
    <col name="LastName" />
    <col name="Age" />
  </cols>
  <rows>
    <row>
      <col>Huey</col>
      <col>Freeman</col>
      <col>10</col>
    </row>
    <row>
      <col>Michael</col>
      <col>Caesar</col>
      <col>10</col>
    </row>
    <row>
      <col>Reiley</col>
      <col>Freeman</col>
      <col>8</col>
    </row>
    <row>
      <col>Cindy</col>
      <col null="true" />
      <col>9</col>
    </row>
    <row>
      <col />
      <col>Robert Jebediah Freeman</col>
      <col>70</col>
    </row>
  </rows>
</data>

      

Is it possible to bind this data to the ListView?

Note that the data columns are not predefined. It can be of any type and the names change.

P / S: I know DataGrid, but it's too heavy to use, we just need to display the data.

0


a source to share


2 answers


You can use the XmlDataProvider for this.

XmlDataProvider data = new XmlDataProvider();

// you can load data from file/http/whatever

data.Source = "... some URL ...";

// *OR* you can go directly against an already existing XmlDocument

// data.Document = someXmlDocumentInstance;

data.XPath = "/books/book"; // specific to your schema obviously

myListView.ItemsSource = data.Data;

      

You can also customize the binding



Binding myBinding = new Binding();

myBinding.Source = data;

BindingOperations.SetBinding(myListView, ItemsControl.ItemsSourceProperty, myBinding);

      

NTN

+2


a source


You can bind this to a list view, however the columns will not be auto-generated. You must either create your own control that does this, or find one that someone else has already done.



if you google you will find all kinds of articles with and without code.

0


a source







All Articles