问题
I have a well formed XML file I would like to fill a datagrid with. I would prefer using the AutoGenerate feature of WFPToolKit datagrid, but could hard code the columns in.
The trouble I am having is getting the xml file contents into a datagrid. I had it partially working in a listview, but think a datagrid would be more suited for my needs.
Can anyone provide a quick example of how to accomplish this?
回答1:
I bound the XML to the ListView like this:
// Bind the data to the ListView
var binding = new System.Windows.Data.Binding() {
Source = MergedXmlDataProvider,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
XPath = "//element" };
this.listView1.SetBinding(ItemsControl.ItemsSourceProperty, binding);
XML looks something like this:
<root>
<element location="here" state="now"/>
<element location="there" state="then"/>
</root>
回答2:
Aha! I finally worked it out with the help of another post here. Here is what I was able to get working, adding each XML element to a list view.
XDocument xdoc = XDocument.Load("c:\\isbn.xml");
var items = from item in xdoc.Descendants("BookData")
select new
{
Title = item.Element("Title").Value,
AuthTexts = item.Element("AuthorsText").Value
};
foreach (var item in items)
{
listView1.Items.Add(new { Title = item.Title, Author = item.AuthTexts });
}
来源:https://stackoverflow.com/questions/1383817/fill-datagrid-or-listview-from-xml-file