Answered Problems with binding DataGrid to XmlDataProvider

  • Saturday, May 26, 2012 1:49 AM
     
      Has Code

    I have the following code:

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

    And here is the XML file that I'm using: http://pastebin.com/5bUCBisB

    What I have achieved so far is somewhat binding the DataGrid to XmlDataProvider - I can tell so much because it autogenerates some columns out of the XML file. The problem is that the manual ones I'm trying to add (displaying name and country of artists from the artist-list array) - nothing shows up in them.

    For what it's worth - a similar approach worked fine in a ListView, I got stuck while migrating.

    What am I doing wrong?

All Replies

  • Saturday, May 26, 2012 9:39 AM
     
     Answered Has Code
    Hi

    to get the prefix "a:" resolved
    the XmlNamespaceManager that defines it needs
    to get attached to the DataGrid (or one of its ancestors)
    using Binding.XmlNamespaceManager

    <DataGrid     
        Binding.XmlNamespaceManager="{StaticResource ns}"
    AutoGenerateColumns="False" 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>



    • Edited by Grecian Developer Saturday, May 26, 2012 10:39 AM
    • Marked As Answer by Kuraj Saturday, May 26, 2012 11:32 AM
    •  
  • Saturday, May 26, 2012 11:32 AM
     
     
    Thanks, worked like a charm!