且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ScrollViewer中为WPF DataGrid使用虚拟化

更新时间:2023-02-09 19:07:25

是的,您已经回答了自己的问题.如果将DataGrid放置在ScrollViewer或StackPanel内,或者任何未将修复约束传递给DataGrid的控件,它将破坏DataGrid中的虚拟化.

Yes, you sort of answered your own question. If you place DataGrid inside ScrollViewer or StackPanel or any control that does not pass fix constraint to DataGrid it will break the virtualization in DataGrid.

您可以通过提供DataGrid.Width和DataGrid.Height修复值来解决此问题.

You can fix this by giving DataGrid.Width and DataGrid.Height fix values.

您需要了解虚拟化的那些修复值.虚拟化需要修复祖先.

You need to know those fix values for the virtualization. Virtualization needs fix ancestors.

这里是给DataGrid修复大小的方法:

Here is how you can give DataGrid fix size:

只需将Height/Widht绑定到ScrollViewer或Grid.

Simply bind Height/Widht to ScrollViewer or Grid.

<Grid x:Name="grid" >
   <ScrollViever>
       <StackPanel>

           <more elements .../>

           <DataGrid Width={Binding ElementName=grid, Path=Width..} Height={Binding ElementName...} />

           <more elements .../>

       </StackPanel>
   </ScrollViever>
<Grid>

就可以了:)