【问题标题】:Fill datagrid or listview from XML file从 XML 文件填充数据网格或列表视图
【发布时间】:2009-09-05 17:18:41
【问题描述】:

我有一个格式良好的 XML 文件,我想用它来填充数据网格。我更喜欢使用 WFPToolKit 数据网格的 AutoGenerate 功能,但可以硬编码其中的列。

我遇到的麻烦是将 xml 文件内容放入数据网格中。我让它在列表视图中部分工作,但认为数据网格更适合我的需求。

谁能提供一个快速的例子来说明如何做到这一点?

【问题讨论】:

    标签: c# wpf xml datagrid wpftoolkit


    【解决方案1】:

    我像这样将 XML 绑定到 ListView:

    // 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 看起来像这样:

    <root>  
        <element location="here" state="now"/>  
        <element location="there" state="then"/>  
    </root> 
    

    【讨论】:

      【解决方案2】:

      啊哈!我终于在这里的另一篇文章的帮助下解决了这个问题。这是我能够开始工作的,将每个 XML 元素添加到列表视图中。

      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 });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多