title: How do I bind a WPF DataGrid to a variable number of columns?
body: <p>My WPF application generates sets of data which may have a different number of columns each time.  Included in the output is a description of each column that will be used to apply formatting.  A simplified version of the output might be something like:</p>
  
  <pre><code>class Data
  {
      IList&lt;ColumnDescription&gt; ColumnDescriptions { get; set; }
      string[][] Rows { get; set; }
  }
  </code></pre>
  
  <p>This class is set as the DataContext on a WPF DataGrid but I actually create the columns programmatically:</p>
  
  <pre><code>for (int i = 0; i &lt; data.ColumnDescriptions.Count; i++)
  {
      dataGrid.Columns.Add(new DataGridTextColumn
      {
          Header = data.ColumnDescriptions[i].Name,
          Binding = new Binding(string.Format("[{0}]", i))
      });
  }
  </code></pre>
  
  <p>Is there any way to replace this code with data bindings in the XAML file instead?</p>