简体   繁体   English

如何在C#中将XML动态绑定到WPF DataGrid

[英]How to Dynamically Bind XML to a WPF DataGrid in C#

I looked around for this, but all the examples I could find used XAML, which made the solution too static. 我四处寻找,但是我能找到的所有示例都使用XAML,这使得解决方案过于静态。 Here is what I want to do: 这是我想做的:

I would like to populate a DataGrid's columns, rows and attributes from an XML file to be specified at run time. 我想从要在运行时指定的XML文件中填充DataGrid的列,行和属性。 Nothing about the DataGrid's attributes can be fixed; 关于DataGrid属性的所有信息都无法修复。 the XML drives it down to the last detail (hence why the XAML examples I saw aren't sufficient). XML将其深入到最后一个细节(因此,为什么我看到的XAML示例不够用)。

The details of the XML file are open, so any layout will do, but as an example: XML文件的详细信息是打开的,因此任何布局都可以,但是例如:

<data>
    <row Column="Col 1" Value="100" />
    <row Column="Col 2" Value ="200" />
</data>

Would yield a grid of 2 columns named Column & Value respectively with the values ("Col 1", 100) & ("Col 2", 200) for the row 1 & 2, respectively. 将产生一个分别名为Column&Value的2列网格,分别具有第1行和第2行的值(“ Col 1”,100)&(“ Col 2”,200)。

Again, I have no problem with radically different XML, so I'll take what works. 同样,对于完全不同的XML我也没有问题,因此我将介绍可行的方法。

Something like this seems very useful as it would allow the creation of generic data viewing components in a variety of domains. 这样的事情似乎非常有用,因为它将允许在各种域中创建通用数据查看组件。 XML would offer a convenient generic format for transmitting structured data and the DataGrid would offer a rich viewing experience. XML将提供一种方便的通用格式来传输结构化数据,而DataGrid将提供丰富的查看体验。

Thank you to everyone who took the time to read or respond to my request. 感谢所有花时间阅读或回复我的请求的人。 I figured out how to do this and am including a code snippet below: 我想出了如何做到这一点,并在下面添加了一个代码片段:

using System.Xml.Linq;    // Required for XElement...
using System.Collections; // Required for Hashtable

private void InitGridFromXML(string xmlPath)
{
    var data = XElement.Load(xmlPath);

    // Set Grid data to row nodes (NOTE: grid = DataGrid member)
    var elements = data.Elements("row");
    grid.ItemsSource = elements;

    // Create grid columns from node attributes.  A hashtable ensures
    // only one column per attribute since this iterates through all
    // attributes in all nodes.  This way, the grid can handle nodes with
    // mutually different attribute sets.
    var cols = new Hashtable();
    foreach (var attr in elements.Attributes())
    {
        var col = attr.Name.LocalName;

        // Only add col if it wasn't added before
        if (!cols.Contains(col))
        {
            // Mark col as added
            cols[col] = true;

            // Add a column with the title of the attribute and bind to its
            // value
            grid.Columns.Add(new DataGridTextColumn
            {
                Header = col,
                Binding = new Binding("Attribute[" + col + "].Value")
            });
        }
    }
}

You could serialize the XML to a collection and bind that collection. 您可以将XML序列化为一个集合并绑定该集合。

You could create a data table from your XML and bind that to the datagrid. 您可以从XML创建数据表,然后将其绑定到数据网格。

As long as you have data in master detail format it doesn't matter what structure you put it in. 只要您具有主要详细信息格式的数据,则采用何种结构都无所谓。

The XML if you make like, XML,如果您喜欢,

presents the data in rows and there columns values. 以行和列值显示数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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