title: WPF DataGrid doesn't seem to be virtualizing rows
body: <p>I'm working on a application where the bulk of my content is presented to users with the built in <code>WPF</code> <code>DataGrid</code>. Here is what my <code>DataGrid</code> looks like. I don't set anything but the <code>RowDefinitions</code> on the parent <code>Grid</code>.</p>
  
  <pre><code>&lt;UserControl xmlns="blah blah blah"&gt;
    &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition /&gt;
        &lt;RowDefinition Height="Auto"/&gt;
       &lt;/Grid.RowDefinitions&gt;
  
    &lt;DataGrid x:Name="dgrMaterialCollection" 
            IsReadOnly="True" 
            ItemsSource="{Binding Path=MyObservableCollection, UpdateSourceTrigger=PropertyChanged}" 
            AutoGenerateColumns="True" 
            AutoGeneratingColumn="dgrMaterialCollection_AutoGeneratingColumn"
            AutoGeneratedColumns="dgrMaterialCollection_AutoGeneratedColumns"
            CanUserResizeColumns="True" 
            CanUserReorderColumns="True"
            CanUserSortColumns="True"
            EnableRowVirtualization="True"
            EnableColumnVirtualization="True"
            VirtualizingPanel.IsVirtualizingWhenGrouping="True"
            VirtualizingPanel.VirtualizationMode="Standard"
            VirtualizingPanel.IsVirtualizing="True"
            SelectionMode="Single"
            SelectionUnit="FullRow"
            ScrollViewer.CanContentScroll="False"
            RowHeaderWidth="0" 
            Grid.Row="0"
            RowHeight="32"
            RowDetailsVisibilityMode="Collapsed"&gt;
    &lt;DataGrid.Resources&gt;
      &lt;ResourceDictionary&gt;
        &lt;ResourceDictionary.MergedDictionaries&gt;
          &lt;ResourceDictionary Source="DataGridStyle.xaml"/&gt;
        &lt;/ResourceDictionary.MergedDictionaries&gt;
  
        &lt;!-- A few value converters --&gt;
       &lt;/ResourceDictionary&gt;
     &lt;/DataGrid.Resources&gt;
     &lt;!-- Some Manually Declared Columns --&gt;
    &lt;/DataGrid&gt;
    &lt;StackPanel Grid.Row="1"&gt;
      &lt;SomeTextBoxes/&gt;
    &lt;/StackPanel&gt;
   &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>When my application displays the <code>DataGrid</code>, there is about a 10 - 20 second delay depending the size of the dataset. During this delay the UI is locked up. Once the data grid is loaded - I can scroll through all few thousand items without any delay. When I say without delay, I mean clicking on the scroll bar and moving it up and down as fast as humanly possible, with no delay in rendering the rows as it scrolls. It seems like the virtualization is not working, and instead the datagrid is creating all the rows at one time instead of while the user is scrolling.</p>
  
  <p>My Datagrid is nested in the following structure (spread out over multiple xaml files). There are no extra <code>ScrollViewers</code>, <code>StackPanels</code>, or anything. Just this.</p>
  
  <pre><code>&lt;RibbonWindow&gt;
    &lt;Grid&gt;
      &lt;ContentControl&gt; &lt;!-- Populated via a DataTemplate and ContentTemplateSelector --&gt;
        &lt;UserControl&gt;
          &lt;UserControl&gt;
            &lt;UserControl&gt;
              &lt;Grid&gt;
                &lt;Here is the DataGrid&gt; 
  </code></pre>
  
  <p>Some things I have attempted to speed up the loading</p>
  
  <ul>
  <li>Explicitly set all the column widths (shaved about 10 second off my load time)</li>
  <li>Turned off <code>AutoGenerateColumns</code> (didn't make a difference).</li>
  <li>Disabling the styles set in the <code>DataGrid's</code> <code>RessourceDictionary</code> (no difference)</li>
  <li>Load items into my <code>ObservableCollection</code> on a background thread, using async/await, and other multithreaded strategies. This has made no difference in load times.</li>
  <li>Add items to a new <code>ObservableCollection</code>, then setting it to the databound property (so the events aren't triggered)</li>
  <li>Add items to the <code>ObservableCollection</code> one at a time (events are triggered upon each add)</li>
  <li>Turning virtualization on and off makes no difference in load time.</li>
  </ul>
  
  <p>I know the delay is not caused by populating the <code>ObservableCollection</code>. It starts after the collection is fully populated, and when the <code>WPF</code> <code>DataGrid</code>is rendering all the rows/columns. I have virtualization enabled and it still takes forever. I am using the .NET framework 4.5.1 (so the 4.6 issue with <code>Virtualization</code>is not causing this). There are also long delays when sorting (using built in datagrid sorting) and resizing columns from the UI.</p>
  
  <p>What am I missing here? Is there something I can do to ensure <code>Virtualization</code>is working? What are some things that might speed this up? If I know <code>Virtualization</code> is working then I may move onto a new strategy, like paging or some kind of lazy loading. Another solution I would be willing to work with is one where the user interface is not blocked/frozen while all the rows are being rendered. That way the users could at least click around on other thing while rows are added in.</p>