DataGrid inside ScrollViewer + UI Virtualization

Nov 6, 2008 at 1:11 PM
Edited Nov 6, 2008 at 1:27 PM

we have a DataGrid with a large number of dynamically added columns. EnableColumnVirtualization and VirtualizingStackPanel.IsVirtualizing is set to True, VirtualizingStackPanel.VirtualizationMode is set to Recycling.

The DataGrid is surrounded by a ScrollViewer with CanContentScroll=True. It looks like the ScrollViewer is forcing the width of the DataGrid to infinity, effectively disabling UI virtualization and crippling performance. Removing the ScrollViewer or setting an explicit width on the DataGrid fixes the issue, but leaves us with a not very functional UI.

How do we ensure that that ScrollViewer gives us a nice horizontal scrollbar, while keeping virtualization enabled?

(I have a hunch that overriding MeasureOverride() can help me fix this problem, but as a newbie to WPF I'm unsure how to go about this. Creating a custom control must surely be overkill?). 

Help, help!
Nov 6, 2008 at 4:57 PM
DataGrid like most of the other ItemsControls comes with its own ScrollViewer and UI Virtualization of rows/columns in tied with that instance of ScrollViewer. If you put DataGrid inside another ScrollViewer with out any restrictions on its size, then by definition of the ScrollViewer, DataGrid would get infinite Height and Width, thus making the inner ScrollViewer in-affective and hence resulting in no virtualization.

Tying UI virtualization to outer instance of ScrollViewer is not impossible, but is pretty advanced. Not only you should know how to Virtualize things, but also you should consider the impact of other controls (surrounding DataGrid) in the ScrollViewer on DataGrid, and it can become really complicated.

I would recommend to change your layout such that you dont have to put DataGrid inside another ScrollViewer.

I can take a shot if you could explain your scenario with more detail.