title: Converting a grid defined via XAML to its C# equivalent
body: <p>I'd like to convert the following XAML sample code into its C# equivalent:</p>
  
  <pre><code>&lt;Grid&gt;
  &lt;Grid.ColumnDefinitions&gt;
      &lt;ColumnDefinition/&gt;
      &lt;ColumnDefinition Width="Auto"/&gt;
      &lt;ColumnDefinition/&gt;
  &lt;/Grid.ColumnDefinitions&gt;
  
  &lt;!-- Sub-grid on left --&gt;
  &lt;Grid Grid.Column="0"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition/&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;Label Content="Left, Row 0" Background="Azure" Grid.Row="0"/&gt;
      &lt;Label Content="Left, Row 2" Background="Lavender" Grid.Row="2"/&gt;
      &lt;GridSplitter Grid.Row="1" Height="8" Background="DarkSlateBlue"
          HorizontalAlignment="Stretch" VerticalAlignment="Center"/&gt;
  &lt;/Grid&gt;
  
  &lt;!-- Sub-grid on right --&gt;
  &lt;Grid Grid.Column="2"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition/&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;Label Content="Right, Row 0" Background="Moccasin" Grid.Row="0"/&gt;
      &lt;Label Content="Right, Row 2" Background="Honeydew" Grid.Row="2"/&gt;
      &lt;GridSplitter Grid.Row="1" Height="8" Background="DarkSlateBlue"
          HorizontalAlignment="Stretch" VerticalAlignment="Center"/&gt;
  &lt;/Grid&gt;
  
  &lt;!-- Splitter between left/right sub-grids --&gt;
  &lt;GridSplitter Grid.Column ="1" Width="8" Background="DarkSlateBlue"
      VerticalAlignment="Stretch" HorizontalAlignment="Center"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is my code so far:</p>
  
  <pre><code>    Grid DynamicGrid = new Grid();
  ColumnDefinition gridColDef0 = new ColumnDefinition();
  ColumnDefinition gridColDef1 = new ColumnDefinition();
  gridColDef1.Width = new GridLength(1, GridUnitType.Auto);
  ColumnDefinition gridColDef2 = new ColumnDefinition();
  DynamicGrid.ColumnDefinitions.Add(gridColDef0);
  DynamicGrid.ColumnDefinitions.Add(gridColDef1);
  DynamicGrid.ColumnDefinitions.Add(gridColDef2);
  RowDefinition gridRowDef0 = new RowDefinition();
  RowDefinition gridRowDef1 = new RowDefinition();
  gridRowDef1.Height = new GridLength(1, GridUnitType.Auto);
  RowDefinition gridRowDef2 = new RowDefinition();
  </code></pre>
  
  <p>But I don't know how to add the grid row definitions to column 0. I'd have expected something along the lines of DynamicGrid.Columns[0].RowDefinitions.Add, but I haven't found such a thing. Can you help me? Thanks!</p>
  
  <p>Edit: For future reference, here's the equivalent C# code. Mind you, it can be optimized!</p>
  
  <pre><code>private void CreateDynamicWPFGrid()
      {
          // Create the Grid
          Grid dynamicGrid = new Grid();
  
          ColumnDefinition gridColDef0 = new ColumnDefinition();
          ColumnDefinition gridColDef1 = new ColumnDefinition();
          gridColDef1.Width = new GridLength(1, GridUnitType.Auto);
          ColumnDefinition gridColDef2 = new ColumnDefinition();
  
          dynamicGrid.ColumnDefinitions.Add(gridColDef0);
          dynamicGrid.ColumnDefinitions.Add(gridColDef1);
          dynamicGrid.ColumnDefinitions.Add(gridColDef2);
  
          // Sub-grid on left.
          {
              Grid leftSubGrid = new Grid();
  
              RowDefinition gridRowDef0 = new RowDefinition();
              RowDefinition gridRowDef1 = new RowDefinition();
              gridRowDef1.Height = new GridLength(1, GridUnitType.Auto);
              RowDefinition gridRowDef2 = new RowDefinition();
  
              leftSubGrid.RowDefinitions.Add(gridRowDef0);
              leftSubGrid.RowDefinitions.Add(gridRowDef1);
              leftSubGrid.RowDefinitions.Add(gridRowDef2);
  
              Label label;
              label = new Label();
              label.Content = "Left, Row 0";
              label.Background = new SolidColorBrush(Colors.Azure);
              Grid.SetRow(label, 0);
              leftSubGrid.Children.Add(label);
  
              label = new Label();
              label.Content = "Left, Row 2";
              label.Background = new SolidColorBrush(Colors.Lavender);
              Grid.SetRow(label, 2);
              leftSubGrid.Children.Add(label);
  
              GridSplitter gridSplitter = new GridSplitter();
              gridSplitter.Height = 8;
              gridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
              gridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
              gridSplitter.VerticalAlignment = VerticalAlignment.Center;
              Grid.SetRow(gridSplitter, 1);
              leftSubGrid.Children.Add(gridSplitter);
  
              Grid.SetColumn(leftSubGrid, 0);
              dynamicGrid.Children.Add(leftSubGrid);
          }
  
          // Sub-grid on right.
          {
              Grid rightSubGrid = new Grid();
  
              RowDefinition gridRowDef0 = new RowDefinition();
              RowDefinition gridRowDef1 = new RowDefinition();
              gridRowDef1.Height = new GridLength(1, GridUnitType.Auto);
              RowDefinition gridRowDef2 = new RowDefinition();
  
              rightSubGrid.RowDefinitions.Add(gridRowDef0);
              rightSubGrid.RowDefinitions.Add(gridRowDef1);
              rightSubGrid.RowDefinitions.Add(gridRowDef2);
  
              Label label;
              label = new Label();
              label.Content = "Left, Row 0";
              label.Background = new SolidColorBrush(Colors.Moccasin);
              Grid.SetRow(label, 0);
              rightSubGrid.Children.Add(label);
  
              label = new Label();
              label.Content = "Left, Row 2";
              label.Background = new SolidColorBrush(Colors.Honeydew);
              Grid.SetRow(label, 2);
              rightSubGrid.Children.Add(label);
  
              GridSplitter gridSplitter = new GridSplitter();
              gridSplitter.Height = 8;
              gridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
              gridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
              gridSplitter.VerticalAlignment = VerticalAlignment.Center;
              Grid.SetRow(gridSplitter, 1);
              rightSubGrid.Children.Add(gridSplitter);
  
              Grid.SetColumn(rightSubGrid, 2);
              dynamicGrid.Children.Add(rightSubGrid);
          }
  
          {
              GridSplitter gridSplitter = new GridSplitter();
              gridSplitter.Width = 8;
              gridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
              gridSplitter.HorizontalAlignment = HorizontalAlignment.Center;
              gridSplitter.VerticalAlignment = VerticalAlignment.Stretch;
              Grid.SetColumn(gridSplitter, 1);
              dynamicGrid.Children.Add(gridSplitter);
          }
  
          // Display grid into a Window
          Content = dynamicGrid;
      }
  </code></pre>