user589195
user589195

Reputation: 4250

Binding XML to wpf datagrid

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>

Upvotes: 2

Views: 2983

Answers (1)

Scott
Scott

Reputation: 12050

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>

Upvotes: 2

Related Questions