title: Programmatically Create Grid with Custom Element
body: <p>I'm trying to create a grid programmatically and appending a custom control as a child to the grid as row 0 column 0 out of a 2x2 matrix. To make matters more tricky, I'm using the MVVM design pattern. Heres some code to help everyone get the idea:</p>
  
  <p><strong>App.xaml.cs</strong></p>
  
  <pre><code>base.OnStartup(e);
  var viewModel = new MainWindowViewModel();
  var mainWindow = new MainWindow();
  mainWindow.GridWindows = viewModel.Window.GridWindows;
  </code></pre>
  
  <p><strong>MainWindowViewModel - method returns the GridWindows.</strong></p>
  
  <pre><code>    private Grid CreateGrid()
      {
          Grid grid = new Grid();
  
          // Create column definitions.
          ColumnDefinition columnDefinition1 = new ColumnDefinition();
          ColumnDefinition columnDefinition2 = new ColumnDefinition();
          columnDefinition1.Width = new GridLength(640);
          columnDefinition2.Width = new GridLength(640);
  
          // Create row definitions.
          RowDefinition rowDefinition1 = new RowDefinition();
          RowDefinition rowDefinition2 = new RowDefinition();
          rowDefinition1.Height = new GridLength(340);
          rowDefinition2.Height = new GridLength(340);
  
          // Attached definitions to grid.
          grid.ColumnDefinitions.Add(columnDefinition1);
          grid.ColumnDefinitions.Add(columnDefinition2);
          grid.RowDefinitions.Add(rowDefinition1);
          grid.RowDefinitions.Add(rowDefinition2);
  
          // Create preview window.
          Border border = new Border();
          border.BorderThickness = new Thickness(20);
          border.Padding = new Thickness(8);
          border.SetResourceReference(Control.BackgroundProperty, "PreviewWindow");
  
          MediaRTSPElement previewElement = new MediaRTSPElement();
          previewElement.Name = "RTSPStreamPlayer";
          previewElement.Stretch = Stretch.UniformToFill;
          previewElement.Source = "rtsp://192.100.100.22/media/video1";
          previewElement.VideoRenderer = VideoRendererType.EnhancedVideoRenderer;
          previewElement.LoadedBehavior = WPFEVR.DirectShow.Players.MediaState.Play;
          previewElement.SpeedRatio = 0.5;
  
          //border.Child = previewElement;
  
          // Add preview window.
          for (int i = 0; i &lt; 4; i++)
          {
              grid.Children.Add(previewElement as UIElement);
              Grid.SetColumn(previewElement, i);
              Grid.SetRow(previewElement, i);
              break;
          }
  
          return grid;
      }
  </code></pre>
  
  <p><strong>And the XAML Markup that the grid should assign to</strong></p>
  
  <pre><code>&lt;Grid x:Name="GridWindows"&gt;&lt;/Grid&gt;
  </code></pre>
  
  <p>The problem is my custom control does not appear in the grid layout, heres the xaml code that does it without code-behind, and this does work:</p>
  
  <pre><code>        &lt;Grid x:Name="GridWindows"&gt;
              &lt;!--&lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="640" /&gt;
                  &lt;ColumnDefinition Width="640" /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="340" /&gt;
                  &lt;RowDefinition Height="340" /&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="0" Grid.Column="0"&gt;
                  &lt;evr:MediaRTSPElement x:Name="RTSPStreamPlayer"
                                Stretch="UniformToFill"
                                VideoRenderer="EnhancedVideoRenderer"
                                LoadedBehavior="Play"
                                Source="rtsp://192.100.100.22/media/video1"
                                SpeedRatio="0.5" /&gt;
              &lt;/Border&gt;
              &lt;Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="0" Grid.Column="1"&gt;
                  &lt;evr:MediaRTSPElement x:Name="RTSPStreamPlayer2"
                                Stretch="UniformToFill"
                                VideoRenderer="EnhancedVideoRenderer"
                                LoadedBehavior="Play"
                                Source="rtsp://192.100.100.78/media/video1"
                                SpeedRatio="0.5" /&gt;
              &lt;/Border&gt;
              &lt;Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="1" Grid.Column="0"&gt;
                  &lt;evr:MediaRTSPElement x:Name="RTSPStreamPlayer3"
                                Stretch="UniformToFill"
                                VideoRenderer="EnhancedVideoRenderer"
                                LoadedBehavior="Play"
                                Source="rtsp://192.100.100.78/media/video1"
                                SpeedRatio="0.5" /&gt;
              &lt;/Border&gt;
              &lt;Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="1" Grid.Column="1"&gt;
                  &lt;evr:MediaRTSPElement x:Name="RTSPStreamPlayer4"
                                Stretch="UniformToFill"
                                VideoRenderer="EnhancedVideoRenderer"
                                LoadedBehavior="Play"
                                Source="rtsp://192.100.100.22/media/video1"
                                SpeedRatio="0.5" /&gt;
              &lt;/Border&gt;--&gt;
          &lt;/Grid&gt;
  </code></pre>
  
  <p>Any ideas as to why programmatic code isn't working?</p>