xmlwpfxamdatagrid

XamDataGrid not populating from XML


I have an existing WPF application that uses an MVVM pattern. I'm attempting to add a new maintenance screen to the application where the user is presented with a combo box of "lookup tables" to choose from. When the user selects a lookup table from the combo box, a XamDataGrid is populated with the data in the selected lookup table. The back end is an MSSQL database with a table called, "LookupTables". Each row in the LookupTables table contains some key columns and a couple of columns of the XML data type; one for the schema and one for the data. So each row in LookupTables represents a complete lookup table that is stored in an XML document. (This is SQL Server 2014, so there is no JSON support, hence the XML.) Each lookup table will have its own unique set of "columns". And the format of the XML is not yet set in stone. Our current design is to have all of the XML schemas start with a <Root> node and have any number of child <Row> nodes. Each node will have some attributes, ex: <Row MyColumn1="some value" MyColumn2="Some other value" />.

I have the combo box working. The data is loaded up from the database correctly. And when the user selects an item I use the following code to attempt to bind the xamDataGrid to the XML data:

        XmlDocument lookupValueDoc = new XmlDocument();
        //lookupValueDoc.LoadXml(SelectedTable.XMLValue);
        // For debugging, I've been ignoring the above line and hard-coding to this...
        string s = "<?xml version='1.0' ?><Root><row test1=\"aaaa\" test2=\"bbbb\"><testa>AAA</testa></row></Root>";
        lookupValueDoc.LoadXml(s);

        XmlDataProvider provider = new XmlDataProvider();
        if (provider != null)
        {
            provider.Document = lookupValueDoc;
            provider.XPath = "/Root";
        }

        SelectedTableData = provider;  // My XamDataGrid is binding to SelectedTableData.

And my XAML for the grid looks like...

<igDP:XamDataGrid Name="LookupGrid" Margin="5"  GroupByAreaLocation="None"  Grid.Row="2" 
            HorizontalAlignment="Left" VerticalAlignment="Top" DataContext="{Binding SelectedTableData}" DataSource="{Binding XPath=Row, Mode=OneWay}"
            BorderThickness="1" BorderBrush="DarkCyan" ScrollViewer.VerticalScrollBarVisibility="Auto" 
            RecordUpdated="LookupGrid_RecordUpdated"
            IsUndoEnabled="True" UndoLimit="50" DataSourceChanged="XamDataGrid_DataSourceChanged"
            Visibility="Visible" >
    <!-- Some other stuff -->
</igDP:XamDataGrid>

But the grid does not seem to be binding. The UI element looks like a small dot where the grid should be. I'm under the assumption that this is the border of the (empty) grid.

I'd appreciate any ideas, I'm running out of things to try. Thanks.


Solution

  • Okay, so I figured out a way to do what I need. Apparently binding to an XmlDataProvider was not the way to go, despite the articles on the Infragistics site and some Microsoft documents. What I found was that if I bind to an XmlNodeList I get the data to show up and the grid fields to dynamically populate. However, there was a catch... The field layout only auto-populates when you bind to a DataSource and the FieldLayouts collection is empty! So you have to run MyGrid.FieldLayouts.Clear(); before re-binding to a new XML data source. Here is some sample code:

    XmlDocument myXmlDoc = new XmlDocument();
    string s = "<Root><row test1=\"aaaa\" test2=\"bbbb\"><testa>AAA</testa></row><row test1=\"AAAA\" test2=\"BBBB\"><testa>XXX</testa></row><row test1=\"A\" test2=\"B\"><testa>X</testa></row></Root>";
    myXmlDoc.LoadXml(s);
    XmlNode root = myXmlDoc.DocumentElement;
    XmlNodeList nodeList = root.SelectNodes("descendant::row");  // This removes the top level of the hierarchy.  It's not required, but met my needs.
    
    MyGrid.FieldLayouts.Clear();
    MyGrid.DataSource = nodeList;