Search code examples
wpfdatagridlistboxui-virtualizationnested-controls

Virtualization of DataGrid inside ListBox


My layout:
enter image description here

Virtualization of outer ListBox works but for inner DataGrid does not. Probably because the outer ListBoxcontains ScrollViewer inside:

The ScrollViewer provides a window onto its child content. The problem is that the child content is given unlimited virtual space. In this virtual space, the DataGrid renders itself at full size, with all of its child items on display.

Looks like there is no simple solution.
But I would like even a workaround. Maybe to limit the virtual space of outer ScrollViewer somehow?


Solution

  • I'm not sure that ScrollViewer is the first to be blamed. ListBox uses StackPanel with Vertical orientation as ItemsPanel. And vertical StackPanel doesn't restrict height of child elements.

    Instead of changing container space, restrict DataGrid size by setting DataGrid.MaxHeight, and StackPanel won't give it more height than that limit. And then ScrollViewer will be limited to total size of all DataGrid.