title: slow loading of controls inside wpf container grid
body: <p>I am creating dynamic UI based on some objects and the loading takes a lot of time because of the large number of items to be rendered.
  See below xml:-</p>
  
  <pre><code> &lt;ScrollViewer Grid.Row="2" ZoomMode="Enabled" x:Name="scrollviewer"  
          VirtualizingStackPanel.VirtualizationMode="Recycling"
          HorizontalScrollMode="Auto"
          VerticalScrollMode="Auto"
          VerticalSnapPointsType="None"
          HorizontalSnapPointsType="None"
          HorizontalScrollBarVisibility="Auto"
          VerticalScrollBarVisibility="Auto"
          MinZoomFactor="1" IsDoubleTapEnabled="True"
          RenderTransformOrigin="0,0"&gt;
          &lt;Grid Background="#FFDDE6EB" x:Name="mygrid" VerticalAlignment="Center" HorizontalAlignment="Center"&gt;
  
          &lt;/Grid&gt;
      &lt;/ScrollViewer&gt;
  </code></pre>
  
  <p>The scrollviewer is inside a row with height as 10*.</p>
  
  <p>Code to load items dynamically is below:-</p>
  
  <pre><code>  public static Grid LoadGridWithItems(Model m)
          {
  
              var grid = new Grid();
              grid.UseLayoutRounding = true;
              grid.RowDefinitions.Clear();
              grid.ColumnDefinitions.Clear();
  
              int count = m.Rows;
              for (int i = 0; i &lt; count; i++)
              {
                  grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
              }
  
              count = m.Columns;
              for (int i = 0; i &lt; count; i++)
              {
                  grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(m.GetColumnWidth(i), GridUnitType.Star) });
              }
              foreach (Control control in m.Controls)
              {
            UIElement uicontrol=null;
                  if (control.Type != null)
                  {
  
                      switch (control.Type.ToLower())
                      {
                          case "textbox":
                               uicontrol=new TextBox();
                              break;
                          case "label":
                              uicontrol = new Label();;
                              break;
  
                        //bunch of other custom controls
                      }
                    }
  
  
                  grid.Children.Add(uicontrol);
              }
              return grid;
          }
  </code></pre>
  
  <p>This generated grid is then added as a child to the "mygrid" defined in above xaml. Sometimes items are more than 5000 and it takes around 20-25 seconds to load. 
  Any suggestions on how to implement this scenario in a better way?</p>
  
  <p>Thanks!</p>