title: WPF Application DataGrid Control Window Switching Lag when lots of Rows in Non-Virtualized DataGrid
body: <p>We have a small .NET 4.0 DataGrid WPF demo with the code posted below. It consists of a non-virtualized DataGrid with 30 columns and 3000 rows. It is non-virtualized, due to us requiring grouping capabilities, which do not support virtualization.</p>
  
  <p>When you run this app, and switch between it and other windows, there is a noticeable lag (of about 1 second). This only occurs when the window is reactivated - once activated, clicking around inside has no associated delay.</p>
  
  <p>We have profiled this lag that occurs on window reactivation using the performance analyzer and have found that there are a lot of dependency property notifications being triggered when the window is brought back into focus. We do not know why this happens, and it seems unnecessary.</p>
  
  <p>We find this delay is proportional to the number of rows in the DataGrid. Does anyone know how we can eliminate or reduce this lag?</p>
  
  <p>UPDATE: It seems like the focus lag occurs even when staying inside the application but focusing on another control such as a textbox outside the grid. Therefore we now know it's not a Window switching problem, but one caused by change in focus, but are still unsure of the exact cause.</p>
  
  <p>(MainWindow.xaml)</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication20.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;DataGrid Name="dataGrid" VirtualizingStackPanel.IsVirtualizing="False" AutoGenerateColumns="True"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>(MainWindow.xaml.cs)</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Windows;
  
  namespace WpfApplication20
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
  
              List&lt;Row&gt; rows = new List&lt;Row&gt;();
  
              for(int i = 0; i &lt; 3000; i++)
              {
                  Row row = new Row(i);
                  rows.Add(row);
              }
  
              dataGrid.ItemsSource = rows;
          }
      }
  
      public class Row
      {
          public double Double1 { get; set; }
          public double Double2 { get; set; }
          public double Double3 { get; set; }
          public double Double4 { get; set; }
          public double Double5 { get; set; }
          public double Double6 { get; set; }
          public double Double7 { get; set; }
          public double Double8 { get; set; }
          public double Double9 { get; set; }
          public double Double10 { get; set; }
          public double Double11 { get; set; }
          public double Double12 { get; set; }
          public double Double13 { get; set; }
          public double Double14 { get; set; }
          public double Double15 { get; set; }
          public double Double16 { get; set; }
          public double Double17{ get; set; }
          public double Double18 { get; set; }
          public double Double19 { get; set; }
          public double Double20 { get; set; }
          public double Double21 { get; set; }
          public double Double22 { get; set; }
          public double Double23 { get; set; }
          public double Double24 { get; set; }
          public double Double25 { get; set; }
          public double Double26 { get; set; }
          public double Double27 { get; set; }
          public double Double28 { get; set; }
          public double Double29 { get; set; }
          public double Double30 { get; set; }
  
          public Row(double d)
          {
              Double1 = d;
              Double2 = d + 1;
              Double3 = d + 2;
              Double4  = d + 3;
              Double5  = d + 4;
              Double6  = d + 5;
              Double7  = d + 6;
              Double8  = d + 7;
              Double9  = d + 8;
              Double10 = d + 9;
              Double11 = d + 10;
              Double12 = d + 11;
              Double13 = d + 12;
              Double14 = d + 13;
              Double15 = d + 14;
              Double16 = d + 15;
              Double17 = d + 16;
              Double18 = d + 17;
              Double19 = d + 18;
              Double20 = d + 19;
              Double21 = d + 20;
              Double22 = d + 21;
              Double23 = d + 22;
              Double24 = d + 23;
              Double25 = d + 24;
              Double26 = d + 25;
              Double27 = d + 26;
              Double28 = d + 27;
              Double29 = d + 28;
              Double30 = d + 29;
          }             
      }                 
  }
  </code></pre>
  
  <p>(Group Style - optionally enabled by putting inside DataGrid XML):</p>
  
  <pre><code>&lt;!--&lt;DataGrid.GroupStyle&gt;
                  &lt;GroupStyle&gt;
                      &lt;GroupStyle.ContainerStyle&gt;
                          &lt;Style TargetType="{x:Type GroupItem}"&gt;
                              &lt;Setter Property="Template"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt;
                                          &lt;Border BorderBrush="DarkGray" BorderThickness="1" Padding="4,0" &gt;
                                              &lt;Expander VerticalContentAlignment="Center" IsExpanded="True"&gt;
                                                  &lt;Expander.Header&gt;
                                                      &lt;Canvas&gt;
                                                          &lt;StackPanel Orientation="Horizontal" Canvas.Top="-11" Canvas.Left="4"&gt;
                                                              &lt;Label Content="{Binding Name}" Visibility="{Binding DataContext.ShowGroupHeaderVisibility, RelativeSource={RelativeSource AncestorType=DataGrid}}"/&gt;
                                                              &lt;Label Content="{Binding ItemCount}" Visibility="{Binding DataContext.ShowGroupCountVisibility, RelativeSource={RelativeSource AncestorType=DataGrid}}"/&gt;
                                                          &lt;/StackPanel&gt;
                                                      &lt;/Canvas&gt;
                                                  &lt;/Expander.Header&gt;
                                                  &lt;ItemsPresenter/&gt;
                                              &lt;/Expander&gt;
                                          &lt;/Border&gt;
                                      &lt;/ControlTemplate&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                          &lt;/Style&gt;
                      &lt;/GroupStyle.ContainerStyle&gt;
                  &lt;/GroupStyle&gt;
              &lt;/DataGrid.GroupStyle&gt;--&gt;
  </code></pre>