【问题标题】:In WPF/Silverlight: Is it possible to bind a DataTable or an XML file to a ListView?在 WPF/Silverlight 中:是否可以将 DataTable 或 XML 文件绑定到 ListView?
【发布时间】:2010-10-24 13:04:54
【问题描述】:

是否可以在 WPF 中将数据矩阵(数据表或 XML 文件)绑定到 ListView?

给定以下 XML 数据集:

<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>

是否可以将此数据绑定到 ListView?

请注意,数据列不是预定义的。它可以是任何类型,名称各不相同。

P/S:我知道 DataGrid,但它对于使用来说太重了,我们只需要显示数据。

【问题讨论】:

    标签: wpf xml silverlight listview datatable


    【解决方案1】:

    您可以将其绑定到列表视图,但列不会自动生成。您必须构建自己的控件来执行此操作,或者找到其他人已经完成的控件。

    如果你google,你会发现各种有代码和无代码的文章。

    【讨论】:

      【解决方案2】:

      您可以为此使用 XmlDataProvider。

      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;
      

      你也可以设置绑定

      Binding myBinding = new Binding();
      
      myBinding.Source = data;
      
      BindingOperations.SetBinding(myListView, ItemsControl.ItemsSourceProperty, myBinding);
      

      HTH

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-20
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 2011-02-17
        • 2011-03-12
        • 2017-01-02
        相关资源
        最近更新 更多