0
votes

I'm using a WPF datagrid (.Net or Toolkit), that is unacceptably slow when binding to an observable collection. It contains roughly 3500 rows and 10 columns and takes over a minute to display the contents. Everything points the the fact that it is not doing UI virtualization of the data. However, I can't figure out why that is the case.

I am not using grouping. I have made sure the grid's height is contained by placing it in a panel with a fixed height. I have set all the virtualization properties on the DataGrid. I have checked in snoop and these properties are set. However, snoop also shows that after loading there are several thousand datagridrows in the visual tree. Whether this is something that is caused by using snoop I have no idea. I have tried using AQTime to find out what is going on. The slowdown does not appear to be in our code but in system code. However, I can't find a way of easily seeing what WPF is up to. I have stripped down the grid and have tried both the .Net 4 DataGrid and the toolkit DataGrid. Both are unacceptably slow to show the initial data. I have tried fixing the row height and column widths. This also makes no difference.

How can I confirm that virtualisation is on and if is is off why it is off? How can I debug what is happening outside of our code? Is there any way of seeing what WPF is up to? (I've tried using the WPF Performance suite, but for some reason it does not give any output for our application).

I'm running out of ideas. It should not be this slow, when only 10 rows are visible in the UI.

Can anyone help?

2
Maybe ScrollViewer.CanContentScroll=false? See Why setting ScrollViewer.CanContentScroll to false disable virtualization.LPL
Thank you very much! This was the reason.Max Palmer
I have added my comment as an answer, so that this question can be closed.LPL

2 Answers

1
votes

Make sure ScrollViewer.CanContentScroll is not set to False.

See Why setting ScrollViewer.CanContentScroll to false disable virtualization for the explanation.

BTW: WPF 4.5 introduces New features for the VirtualizingPanel which may help you.

1
votes

Link in the accepted answer relating to virtualization features in WPF 4.5 is dead, but here's a summary.

If you're using WPF 4.5, you can get pixel-based scrolling AND virtualization by leaving ScrollViewer.CanContentScroll="true" (i.e. not changing it from the default) and setting VirtualizingStackPanel.ScrollUnit="Pixel". Example:

<ListView ItemsSource="{Binding YourItems}"
    ItemTemplate="{StaticResource YourListItemTemplate}"
    VirtualizingStackPanel.ScrollUnit="Pixel"/>

We were seeing an average of 4.7 seconds to render 200 instances of a complex DataTemplate consisting of text boxes, combo boxes and buttons, but by re-enabling CanContentScroll and setting the ScrollUnit instead took the render time down to 0.08 seconds.