title: Dynamically bind grid in WPF
body: <p>I am not able to view rows in my grid which I am binding to it dynamically here is my code</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
  
          DisplayGrid();
      }
  
      private void DisplayGrid()
      {
          var records = new ObservableCollection&lt;Record&gt;();
          records.Add(new Record(new Property("FirstName", "ABC"), new Property("LastName", "DEF")));
          records.Add(new Record(new Property("FirstName", "GHI"), new Property("LastName", "JKL")));
  
          var columns = records.First()
              .Properties
              .Select((x, i) =&gt; new { Name = x.Name, Index = i })
              .ToArray();
  
          foreach (var column in columns)
          {
              var binding = new Binding(string.Format("Properties[{0}].Value", column.Index));
              dataGrid.Columns.Add(new DataGridTextColumn() { Header = column.Name, Binding = binding });
          }
      }
  }
  
  class Record
  {
  
      readonly ObservableCollection&lt;Property&gt; _properties = new ObservableCollection&lt;Property&gt;();
  
      public Record(params Property[] properties)
      {
          foreach (var property in properties)
          {
              _properties.Add(property);
          }
      }
  
      public ObservableCollection&lt;Property&gt; Properties
      {
          get { return _properties; }
      }
  
  
  }
  </code></pre>
  
  <p>and in my XAML</p>
  
  <pre><code>       &lt;DataGrid
          Name="dataGrid"
          AutoGenerateColumns="False"
          ItemsSource="{Binding Path=Records}"/&gt;
  </code></pre>
  
  <p>I am only able to display headers in my grid but not the rows..</p>
  
  <p>Thanks</p>