ListBox 内部 DataGrid 的虚拟化

Virtualization of DataGrid inside ListBox

我的布局:

外部 ListBox 的虚拟化有效,但内部 DataGrid 的虚拟化无效。可能是因为外面的ListBox里面有ScrollViewer

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.

看来没有简单的解决办法。
但我什至想要一个解决方法。也许以某种方式限制外部 ScrollViewervirtual space?

我不确定 ScrollViewer 是第一个被指责的。 ListBox 使用垂直方向的 StackPanel 作为 ItemsPanel。垂直 StackPanel 不限制子元素的高度。

不更改容器 space,而是通过设置 DataGrid.MaxHeight 来限制 DataGrid 的大小,StackPanel 不会为其提供超过该限制的高度。然后 ScrollViewer 将被限制为所有 DataGrid 的总大小。

刚在这里找到类似的答案DataGrid inside Datagrid virtualization

Can you try fixing the height of the inner datagrid. That way only a portion of it would be displayed with scrolling enabled inside it. This will allow virtualization of the inner datagrid since only a portion of it is rendered at a time and the rest would be dynamically rendered during scrolling and it will increase the speed with which your datagrid's are loaded.

如果有帮助,请按照上面的link为他的回答点赞