0

I'm having performance problems with a datagrid which I build dynamically. I have 8 frozen columns and the rest are created dynamically.

For testing I use ~1000 rows and 100 columns. I do not use data virtualization because the whole collection of data is roughly 25MB. I am however having problems with performance while scrolling. I know this is a very common problem and I have read a few useful resources, including the 'Optimizing WPF Application Performance' article on the MSDN website and other solution presented here on StackOverflow.

Also, the datagrid's parent container is a Grid, not a StackPanel (I read that the datagrid will render all rows, even if they're not visible).

The XAML for my datagrid is this:

<cg:ColouredDataGrid Grid.Column="0"
        x:Name="usagesDataGrid"
        AutoGenerateColumns="False"
        SelectionUnit="Cell"
        ItemsSource="{Binding Records}"
        Block.TextAlignment="Center"
        VirtualizingStackPanel.IsVirtualizing="true"
        VirtualizingStackPanel.VirtualizationMode="Recycling"
        ScrollViewer.IsDeferredScrollingEnabled="True" DataContext="{Binding}"
        ScrollViewer.CanContentScroll="True"
        EnableColumnVirtualization="False" .../>

ColouredDataGrid is a custom class I created in which I override the PrepareContainerForItemOverride method so I can conditionally apply custom coloring to the rows.

The problem is that when I debug, every time I scroll, the PrepareContainerForItemOverride method fires for each new row that becomes visible. Shouldn't it not fire, since the VirtualizationMode is set to Recycling? Shouldn't it reuse the containers already created for those items?

Can someone please explain to me what I'm doing wrong?

Thanks a lot.

Adrian
  • 735
  • 9
  • 26
  • I know setting `VirtualizingStackPanel.IsVirtualizing` is not enough to turn on virtualization. See [this question](http://stackoverflow.com/q/2783845/302677) for more information. DataGrids should enable virtualization by default. Just be sure their height is limited so they show their scrollbar – Rachel Jan 18 '12 at 15:55
  • The height is limited, I do have scrollbars and the 'ScrollViewer.CanContentScroll' property is set to 'True'. – Adrian Jan 19 '12 at 06:54
  • I removed my answer on rereading your question, it seems I misunderstood your problem a bit :) Giving it a second thought I think it is expected that the event is fired when the container is filled with the new elements. I guess you can optimise your override to return if there is part of it which shouldn't be redone on scrolling. – dain Jan 19 '12 at 23:52
  • Ok, so let me get this straight... Redrawing the container is not the same as recreating it right? What I'm doing is redrawing the container, since it could have a different background color, depending on which row used it before? – Adrian Jan 20 '12 at 07:04

0 Answers0