title: How to make adding controls in a WPF application faster
body: <p>I'm working on a WPF app that lets users edit data records.</p>
  <p>For a test, I'm adding 50 rows to a tree view and this takes ~200ms.
  This creates noticable stutter as the application isn't interactive during that time.
  This is only the time for creating and populating controls, no data loading or any work that could be done in a thread.</p>
  <p>Since all these rows fit on a screen, I think it would not benefit from making it a virtualizing panel.</p>
  <p>Is it possible to make this faster? How would I add these over multiple &quot;frames&quot;? How can I profile this? How can I determine a reasonable number of controls that my WPF app should be able to render?</p>
  <p>Edit: Adding a minimal example to reproduce.</p>
  <p>MainWindow.xaml:</p>
  <pre><code>&lt;Window x:Class=&quot;WpfApp1.MainWindow&quot;
          xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
          xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
          xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
          xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
          mc:Ignorable=&quot;d&quot;
          Title=&quot;MainWindow&quot; Height=&quot;450&quot; Width=&quot;800&quot;&gt;
      &lt;DockPanel&gt;
          &lt;StackPanel DockPanel.Dock=&quot;Top&quot; Orientation=&quot;Horizontal&quot; Margin=&quot;10&quot;&gt;
              &lt;Button Width=&quot;100&quot; Click=&quot;Button_Click&quot;&gt;Test&lt;/Button&gt;
              &lt;TextBlock Margin=&quot;10&quot; x:Name=&quot;resultTextBox&quot;&gt;Result&lt;/TextBlock&gt;
          &lt;/StackPanel&gt;
          &lt;TreeView x:Name=&quot;myTreeView&quot;&gt;&lt;/TreeView&gt;
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  <p>MainWindow.cs:</p>
  <pre><code>using System.Diagnostics;
  using System.Windows;
  
  namespace WpfApp1
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              var myStopwatch = new Stopwatch();
              myStopwatch.Start();
  
              this.myTreeView.Items.Clear();
              for (int i = 0; i &lt; 50; i++)
              {
                  this.myTreeView.Items.Add(new MyTreeViewItem());
              }
  
              myStopwatch.Stop();
              this.resultTextBox.Text = &quot;It took &quot; + myStopwatch.ElapsedMilliseconds + &quot; ms to add 50 tree view items.&quot;;
          }
      }
  }
  </code></pre>
  <p>MyTreeViewItem.xaml:</p>
  <pre><code>&lt;TreeViewItem x:Class=&quot;WpfApp1.MyTreeViewItem&quot;
               xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
               xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
               xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; 
               xmlns:local=&quot;clr-namespace:WpfApp1&quot;
               mc:Ignorable=&quot;d&quot; 
               d:DesignHeight=&quot;450&quot; d:DesignWidth=&quot;800&quot;&gt;
      &lt;TreeViewItem.Header&gt;
          &lt;StackPanel Orientation=&quot;Horizontal&quot; VerticalAlignment=&quot;Center&quot;&gt;
              &lt;TextBlock&gt;I'm a TextBlock&lt;/TextBlock&gt;
              &lt;Button&gt;I'm a button&lt;/Button&gt;
              &lt;CheckBox&gt;I'm a checkbox&lt;/CheckBox&gt;
              &lt;TextBox&gt;I'm a text box&lt;/TextBox&gt;
              &lt;ComboBox SelectedIndex=&quot;0&quot;&gt;
                  &lt;ComboBox.Items&gt;
                      &lt;ComboBoxItem&gt;I'm a combobox&lt;/ComboBoxItem&gt;
                  &lt;/ComboBox.Items&gt;
              &lt;/ComboBox&gt;
          &lt;/StackPanel&gt;
      &lt;/TreeViewItem.Header&gt;
      &lt;TreeViewItem.Items&gt;
          &lt;TreeViewItem Visibility=&quot;Collapsed&quot;&gt;&lt;/TreeViewItem&gt;
      &lt;/TreeViewItem.Items&gt;
  &lt;/TreeViewItem&gt;
  </code></pre>
  <p>Screenshot:
  <a href="https://i.stack.imgur.com/EClyT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EClyT.png" alt="enter image description here" /></a></p>
  <p>According to the VS profiler, it takes an additional 150ms for the Layout step after adding the items.</p>