title: Create DataGridRowsDetails in code behind WPF
body: <p>i'm working on a new program that will show information about Test on some Tools in my factory.
  For doing that i'm creating a DataGrid Control at run time and populate it.This part is working very well.</p>
  
  <p>My problem is to add RowDetails, How can i do that in code behind? </p>
  
  <pre><code>if (datatable.Rows.Count != 0)
      {
        foreach (DataRow dt in datatable.Rows)
          {
            if (last_tool_test == "" || last_tool_test != dt[1].ToString())
               {
                 last_tool_test = dt[1].ToString();
                 txt = new TextBlock();
                 txt.Text = dt[1].ToString() + " :";
                 txt.TextDecorations = TextDecorations.Underline;
                 txt.HorizontalAlignment = HorizontalAlignment.Center;
                 txt.Margin = new Thickness(0, 0, 0, 7);
  
                 data = new DataGrid();
                 data.Width = double.NaN;
                 data.Margin = new Thickness(5, 0, 5, 5);
                 data.HeadersVisibility = DataGridHeadersVisibility.Column;
                 data.CanUserAddRows = false;
                 data.CanUserDeleteRows = false;
                 data.CanUserReorderColumns = false;
                 data.CanUserResizeColumns = false;
                 data.CanUserSortColumns = false;
                 data.IsReadOnly = true;
                 data.SelectionMode = DataGridSelectionMode.Extended;
                 DataGridTextColumn col1 = new DataGridTextColumn();
                 col1.Header = "Recipe Name";
                 col1.Binding = new Binding("Recipe_Name");
                 col1.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
                 var style = new Style(typeof(DataGridColumnHeader));
                 style.Setters.Add(new Setter()
                 {
                  Property = HorizontalAlignmentProperty,
                  Value = HorizontalAlignment.Stretch
                 });
                 style.Setters.Add(new Setter()
                 {
                  Property = HorizontalContentAlignmentProperty,
                  Value = HorizontalAlignment.Center
                 });
                 style.Setters.Add(new Setter()
                 {
                  Property = FontWeightProperty,
                  Value = FontWeights.Bold
                 });
                 var style1 = new Style(typeof(TextBlock));
                 style1.Setters.Add(new Setter()
                 {
                  Property = HorizontalAlignmentProperty,
                  Value = HorizontalAlignment.Center
                 });
                 col1.HeaderStyle = style;
                 col1.ElementStyle = style1;
                 data.Columns.Add(col1);
                 DataGridTextColumn col2 = new DataGridTextColumn();
                 col2.Header = "Foup";
                 col2.Width = 100;
                 col2.Binding = new Binding("Foup");
                 col2.HeaderStyle = style;
                 col2.ElementStyle = style1;
                 data.Columns.Add(col2);
                 DataGridTextColumn col3 = new DataGridTextColumn();
                 col3.Header = "Need Reference?";
                 col3.Width = 110;
                 col3.HeaderStyle = style;
                 col3.ElementStyle = style1;
                 col3.Binding = new Binding("Reference");
                 data.Columns.Add(col3);
                 DataGridTextColumn col4 = new DataGridTextColumn();
                 col4.Header = "# Iteration";
                 col4.Width = 100;
                 col4.HeaderStyle = style;
                 col4.ElementStyle = style1;
                 col4.Binding = new Binding("Iteration");
                 data.Columns.Add(col4);
                 DataGridTextColumn col5 = new DataGridTextColumn();
                 col5.Header = "Spec";
                 col5.Width = 100;
                 col5.HeaderStyle = style;
                 col5.ElementStyle = style1;
                 data.Columns.Add(col5);
                 col5.Binding = new Binding("Spec");
                 st.Children.Add(txt);
                 st.Children.Add(data);
               }                                    
              data.Items.Add(new Tool_Test() { Recipe_Name = dt[2].ToString(), Foup = dt[3].ToString(), Reference = dt[4].ToString(), Iteration = dt[5].ToString(), Spec = dt[6].ToString() });
          }
          last_tool_test = "";
        }
     }
  </code></pre>
  
  <p>edit: I think i found a solution by using resource:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;DataTemplate x:Key="CustomTemplate"&gt;
          &lt;DataGrid x:Name="datagrid1" HeadersVisibility="Column" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" SelectionMode="Extended" Margin="5,0,5,50" CanUserResizeColumns="False" AutoGenerateColumns="False" VerticalAlignment="Top" IsReadOnly="True" Width="750"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Header="Sub Test Name" Width="*" Binding="{Binding Path=Recipe_Name}"&gt;
                      &lt;DataGridTextColumn.HeaderStyle&gt;
                          &lt;Style TargetType="DataGridColumnHeader"&gt;
                              &lt;Setter Property="HorizontalAlignment" Value="Stretch"/&gt;
                              &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt;
                              &lt;Setter Property="FontWeight"  Value="Bold" /&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridTextColumn.HeaderStyle&gt;
                      &lt;DataGridTextColumn.ElementStyle&gt;
                          &lt;Style TargetType="TextBlock"&gt;
                              &lt;Setter Property="HorizontalAlignment" Value="Center" /&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridTextColumn.ElementStyle&gt;
                  &lt;/DataGridTextColumn&gt;
                  &lt;DataGridTextColumn Header="Spec" Width="*" Binding="{Binding Path=Foup}"&gt;
                      &lt;DataGridTextColumn.HeaderStyle&gt;
                          &lt;Style TargetType="DataGridColumnHeader"&gt;
                              &lt;Setter Property="HorizontalAlignment" Value="Stretch"/&gt;
                              &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt;
                              &lt;Setter Property="FontWeight"  Value="Bold" /&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridTextColumn.HeaderStyle&gt;
                      &lt;DataGridTextColumn.ElementStyle&gt;
                          &lt;Style TargetType="TextBlock"&gt;
                              &lt;Setter Property="HorizontalAlignment" Value="Center" /&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridTextColumn.ElementStyle&gt;
                  &lt;/DataGridTextColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/DataTemplate&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>and in my code i added this:</p>
  
  <pre><code>data.RowDetailsTemplate = (DataTemplate)this.Resources["CustomTemplate"];
  </code></pre>
  
  <p>Now it's half working. I don't have experience with resource so i can't find a solution to Bind data to it. How can i do that?</p>
  
  <p>Thank you for your help.</p>