title: WPF - UserControls very slow
body: <p>I am designing something similar a PropertyGrid where I want to show properties of objects. For special reasons I am not going to use the PropertyGrid but create my own.</p>
  
  <p>For each property I have created a custom usercontrol. Now to my horror the performance is very bad. If I have something like 100 properties it takes 500 milliseconds to show them in a StackPanel/Listbox.</p>
  
  <p>I did an experiment where I add 200 default UserControls to a StackPanel. It took about 50 milliseconds. Still a very high number I think.</p>
  
  <p>Should I not use usercontrols for such a purpose? It seems very object-oriented to do it this way and I can not really see another solution.</p>
  
  <p>However I can see that PropertyGrid and TreeView performs good, so what have they done and what should I do? </p>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>        Stopwatch stopwatch = new Stopwatch();
          stopwatch.Start();
          using (var suspend = Dispatcher.DisableProcessing())
          {
              // Add all children here
              for (int i = 0; i &lt; 200; i++)
              {
                  this.propertiesStackPanel.Children.Add(new System.Windows.Controls.Button(){Content = "Testing"});
              }
          }
          stopwatch.Stop();
  </code></pre>
  
  <p>This still takes about 50 milliseconds. If I change to my own custom usercontrol it is much higher. I might add that scrolling is not a problem.</p>
  
  <p><strong>Edit2:</strong></p>
  
  <p>OK. It has nothing to do with stackpanel. I have found out that it is because creating UserControls is a very expensive operation. If you have any other idea of what to do I would gladly hear them :)</p>
  
  <p><strong>Edit3:</strong>
  Nothing is going on in the constructor of my usercontrol other than InitializeComponent method. Here is an example of a usercontrol I am adding.</p>
  
  <pre><code>&lt;UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  x:Class="PropertyBox.GroupUC"
  x:Name="UserControl"
  d:DesignWidth="640" d:DesignHeight="480" Background="#FF32B595" BorderThickness="0"&gt;
  
  &lt;Grid x:Name="LayoutRoot"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="20px"/&gt;
          &lt;ColumnDefinition Width="*"/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Border x:Name="border" BorderThickness="0,1" Grid.Column="1"&gt;
          &lt;TextBox Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Right" BorderThickness="0" Padding="0" Visibility="Hidden"/&gt;
      &lt;/Border&gt;
      &lt;Label x:Name="groupNameLabel" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center" Content="Label" Padding="0" Grid.Column="1"/&gt;
      &lt;Button x:Name="expandButton" HorizontalAlignment="Left" VerticalAlignment="Center" Width="12" Height="12" Content="" Click="ExpandButtonClick" Margin="4,0,0,0" Padding="0" Grid.ColumnSpan="2" d:IsHidden="True"/&gt;
      &lt;Image x:Name="expandButton2" Visibility="Hidden"  Width="12" Height="12" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>