title: How to dynamically create a datagrid in WPF?
body: <p>I have the following datagrid in XAML:</p>
  
  <pre><code>&lt;DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False" IsReadOnly="True"
            GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" 
            CanUserResizeColumns="False" CanUserResizeRows="False" 
            CanUserReorderColumns="False" &gt;
      &lt;DataGrid.ColumnHeaderStyle&gt;
          &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
              &lt;Setter Property="FontWeight" Value="Bold" /&gt;
              &lt;Setter Property="FontSize" Value="12" /&gt;
          &lt;/Style&gt;
      &lt;/DataGrid.ColumnHeaderStyle&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="Type" Width="200" FontSize="12" 
                              Binding="{Binding Path=Name}" /&gt;
          &lt;DataGridTemplateColumn Header="Ingredients" Width="*"&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                    &lt;DataGrid ItemsSource="{Binding Ingredients}" 
                              AutoGenerateColumns="False" IsReadOnly="True"
                              GridLinesVisibility="None" CanUserAddRows="False" 
                              CanUserDeleteRows="False" CanUserResizeColumns="False"
                              CanUserResizeRows="False" CanUserReorderColumns="False" &gt;
                          &lt;DataGrid.ColumnHeaderStyle&gt;
                              &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
                                  &lt;Setter Property="FontWeight" Value="Bold" /&gt;
                                  &lt;Setter Property="FontSize" Value="12" /&gt;
                              &lt;/Style&gt;
                          &lt;/DataGrid.ColumnHeaderStyle&gt;
                          &lt;DataGrid.Columns&gt;
                              &lt;DataGridTextColumn Header="Ingredients" 
                                              Width="*" FontSize="12"
                                              Binding="{Binding Path=IngredientName}"/&gt;
                              &lt;DataGridTextColumn Header="Quantite" Width="*" 
                                            FontSize="12" Binding="{Binding Path=Qty}"/&gt;
                          &lt;/DataGrid.Columns&gt;
                      &lt;/DataGrid&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>I am trying to find a way to create the datagrid dynamically (in-code) so that I can create multiple copies of it and bind it to different datasources at run-time.  </p>
  
  <p>Is this possible?  Anyone know how I could go about it for a datagrid complicated like this?</p>