使用自动列大小时 WPF 数据网格速度变慢
当我在下面的代码中展开 Expander 时,应用程序变得非常缓慢,需要 2-3 秒才能响应用户触发的调整大小/移动事件。如果我将第二列设置为
响应时间仍然是最佳的。我在这里缺少什么? (我需要如下所示的 UI,但没有迟缓)
<UserControl>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Toolbar .. />
<DataGrid Grid.Row="1" ItemsSource="{Binding collection1}" .. />
<Expander Grid.Column="1" Grid.RowSpan="2">
<DataGrid ItemsSource="{Binding collection2}" .. />
</Expander>
</Grid>
</UserControl>
仅供参考:我怀疑当设置 Width=Auto 并且为整个绑定数据源创建 DataGridRow 对象时,未使用行虚拟化...
更新 以下内容也不能消除迟缓;
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width={Binding ElementName=expander, Path=Width} />
</Grid.ColumnDefinitions>
..
<Expander Name="expander" Grid.Column="1" Grid.RowSpan="2">
<DataGrid ItemsSource="{Binding collection2}" Width="300" .. />
</Expander>
When I expand the Expander in the code below, the application becomes very sluggish and takes 2-3 seconds to respond to resize/move events triggered by the user. If I set the second column to <ColumnDefinition Width="250"/>
response time remains optimal. What am I missing here? (I'm need the UI as per below but without the sluggishness)
<UserControl>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Toolbar .. />
<DataGrid Grid.Row="1" ItemsSource="{Binding collection1}" .. />
<Expander Grid.Column="1" Grid.RowSpan="2">
<DataGrid ItemsSource="{Binding collection2}" .. />
</Expander>
</Grid>
</UserControl>
FYI: I suspect Row Virtualization is not being used when Width=Auto is set and DataGridRow objects are being created for the entire bound data source...
UPDATE
The following also does not remove the sluggishness;
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width={Binding ElementName=expander, Path=Width} />
</Grid.ColumnDefinitions>
..
<Expander Name="expander" Grid.Column="1" Grid.RowSpan="2">
<DataGrid ItemsSource="{Binding collection2}" Width="300" .. />
</Expander>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,如果将
ColumnWidth
设置为Auto
,则将根据内容(此处为 dataGrid)计算列宽。因此,您的第二列将非常大(即使您可能看不到它,具体取决于您的布局),并且每次都会绘制 dataGrid 的列。所以基本上,您失去了
columnVirtualization
(而不是rowVirtualization
)的好处。这与您永远不应将
DataGrid
放入ScrollViewer
的原因相同。解决方案
在网格上设置
VirtualizingStackPanel.IsVirtualizing="True"
如果这不起作用,您可能需要自己调整大小,没有真正的选择。
The problem is that if you set the
ColumnWidth
toAuto
, the column width will be calculated depending on the content, here, the dataGrid. So you will have the second column very large (even though you might not see it depending on your layout) and the dataGrid's columns will all be drawn every time.So basically, you loose the benefits of
columnVirtualization
(notrowVirtualization
).This is the same reason why you should never put a
DataGrid
into aScrollViewer
.Solution
Set
VirtualizingStackPanel.IsVirtualizing="True"
on your gridif this does not work, you might have to take care of the resizing yourself, no real option there.