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.