title: Binding complex objects to a DataGrid
body: <p>Say I have an ObservableCollection (type MyRow) (called Rows) of rows. In MyRow I have a bunch of properties, including an ObservableCollection (type MyRowContents) (called Contents) - which is basically a list of all of the contents that will be in that row. MyRowContents also has several properties, including DisplayContents (string).</p>
  
  <p>For example... for DataGrid with 2 rows, and 3 columns the data structure would be 2 "Rows", and each would contain 3 "Contents".</p>
  
  <p>What do I have to do in the DataGrid in the XAML so that the cell contents display Rows[i].Contents[j].DisplayContents?</p>
  
  <p>EDIT:</p>
  
  <p>Hi, sorry for not being clear enough in the initial message. I am trying to make a data grid that is capable of displaying data dynamically (from a database table that is unknown ahead of time).</p>
  
  <p>I have used this as a guide: <a href="http://pjgcreations.blogspot.com.au/2012/09/wpf-mvvm-datagrid-column-collection.html" rel="nofollow">http://pjgcreations.blogspot.com.au/2012/09/wpf-mvvm-datagrid-column-collection.html</a> - and I can display the columns dynamically.</p>
  
  <pre><code>    &lt;DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="False" 
                w:DataGridExtension.Columns="{Binding ElementName=LayoutRoot, Path=DataContext.Columns}" /&gt;
  </code></pre>
  
  <p>I just don't know how to display the row data dynamically.</p>
  
  <p>MyRow is:</p>
  
  <pre><code>public class MyRow
  {
      public int UniqueKey { get; set; }
      public ObservableCollection&lt;MyRowContents&gt; Contents { get; set; }
  }
  </code></pre>
  
  <p>MyRowContents is:</p>
  
  <pre><code>public class MyRowContents
  {
      public string ColumnName { get; set; } // Do I need this?
      public object DisplayContents { get; set; } // What I want displayed in the grid cell
  }
  </code></pre>
  
  <p>I have a public property ObservableCollection Rows in my view model, which as you can see is what I am binding to in the ItemsSource. But I don't know how to get the "DisplayContents" of each MyRowContents to display in the appropriate cell in the grid view.</p>
  
  <p>Given that I can display the column names dynamically, this assumption can be made: the order of the items in Contents (in MyRow) will match the order of the columns.</p>
  
  <p>Thanks</p>