【问题标题】:Problems with binding DataGrid to XmlDataProvider将 DataGrid 绑定到 XmlDataProvider 的问题
【发布时间】:2012-06-01 12:43:36
【问题描述】:

我有以下代码:

<Window.Resources>
    <XmlNamespaceMappingCollection x:Key="ns">
        <XmlNamespaceMapping Prefix="a" Uri="http://musicbrainz.org/ns/mmd-2.0#" />
    </XmlNamespaceMappingCollection>
    <XmlDataProvider XmlNamespaceManager="{StaticResource ns}" x:Key="SearchArtistDataSource" XPath="/a:metadata/a:artist-list/a:artist" Source="file.xml" />
</Window.Resources>
<DataGrid x:Name="SearchArtistDataGrid" ItemsSource="{Binding Source={StaticResource SearchArtistDataSource}}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding XPath=a:name}" />
        <DataGridTextColumn Binding="{Binding XPath=a:country}" />
    </DataGrid.Columns>
</DataGrid>

这是我正在使用的 XML 文件:http://pastebin.com/5bUCBisB

到目前为止,我所做的只是将 DataGrid 绑定到 XmlDataProvider - 我可以说很多,因为它会从 XML 文件中自动生成一些列。问题是我正在尝试添加的手动(显示艺术家列表数组中艺术家的姓名和国家/地区) - 没有任何内容显示在其中。

对于它的价值 - 类似的方法在 ListView 中运行良好,但我在迁移时卡住了。

我做错了什么?

【问题讨论】:

    标签: c# wpf wpf-controls wpfdatagrid


    【解决方案1】:

    我在 MSDN 论坛上得到了答案……这个问题事后诸葛亮。

    XmlNamespaceManager 不仅要附加到 XmlDataProvider,还要附加到 DataGrid 本身:

    <DataGrid Binding.XmlNamespaceManager="{StaticResource ns}">
    

    希望这对遇到类似问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多