2

I have a WPF data grid and an xmlString and am having problems binding it.

I have read this post and followed the instructions but cant get it to work.

I see the column with the title but no data is in there. Any ideas?

I should say the code behind is called when a button is clicked after the window has loaded.

Here is my XAML

<DataGrid x:Name="dtgMain" ItemsSource="{Binding Path=Elements[panel]}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="PanelCode" Binding="{Binding Path=Element[panelCode].InnerText}"/>
    </DataGrid.Columns>
</DataGrid>

Here is my codebehind

System.IO.StringReader reader = new System.IO.StringReader(response);
            XElement results = XElement.Load(reader);
            dtgMain.DataContext = results;

here is my xml

<data>
  <load count="2">true
    <panel index="10">
      <panelCode>100072
      </panelCode>
      <panelName>COM0100072*A
      </panelName>
      <panelPart>100072
      </panelPart>
      <numberLayers>4
      </numberLayers>
      <panelSize.display>21.0 x 24.0
      </panelSize.display>
      <lastEdited.display>16/11/2011 17:13:39
      </lastEdited.display>
      <panelStatus.display>Measurements Stored
      </panelStatus.display>
      <lastEditor>admin
      </lastEditor>
      <numberBonds>1
      </numberBonds>
      <numberSubComponents>0
      </numberSubComponents>
    </panel>
    <panel index="11">
      <panelCode>100352
      </panelCode>
      <panelName>COM0100352*C
      </panelName>
      <panelPart>100352
      </panelPart>
      <numberLayers>8
      </numberLayers>
      <panelSize.display>18.0 x 24.0
      </panelSize.display>
      <lastEdited.display>16/11/2011 17:18:47
      </lastEdited.display>
      <panelStatus.display>Measurements Stored
      </panelStatus.display>
      <lastEditor>admin
      </lastEditor>
      <numberBonds>1
      </numberBonds>
      <numberSubComponents>0
      </numberSubComponents>
    </panel>
  </load>
</data>
4
  • In my opinion you should be using the XmlDocument family of XML classes as they support Binding.XPath. Commented Jan 13, 2012 at 19:11
  • H.B would you please be able to post a code example or at least link to some examples? Commented Jan 14, 2012 at 10:04
  • Logically there is an example in the XPath property documentation i linked to. Commented Jan 14, 2012 at 13:59
  • HB there are 2 things that stick out with that example. The first is that its using a static xml file instead of a programatically generated on as in my example. The second thing is that its bingding to a list box instead of a datagrid. Would you be able to give examples of how to use this in my situation? Commented Jan 16, 2012 at 9:32

1 Answer 1

2

XElement does not have an 'InnerText' property.

Have you tried using

Binding="{Binding Path=Element[panelCode].Value}"

instead?

Edit:

I just tested with a sample project and the only other change (besides using .Value) is changing your ItemsSource of your DataGrid to include the 'load' node:

ItemsSource="{Binding Path=Element[load].Elements[panel]}"

So your data grid will look like the following:

<DataGrid x:Name="dtgMain" ItemsSource="{Binding Path=Element[load].Elements[panel]}">
    <DataGrid.Columns>
       <DataGridTextColumn Header="PanelCode" Binding="{Binding Path=Element[panelCode].Value}"/>
    </DataGrid.Columns>
</DataGrid>
Sign up to request clarification or add additional context in comments.

3 Comments

I started off using that and am sure I had the same situation. I will change and show the result :D
Using value results in the same situation of nothing being displayed.
I've updated my answer to include a solution that I have tested (in .NET 4) and verified it works. It appears the initial <data> node is treated as the root, but you cannot just ignore the <load> node and skip straight to <panel> as you were doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.