使用自动列大小时 WPF 数据网格速度变慢

发布于 2024-10-17 21:20:46 字数 1277 浏览 0 评论 0原文

当我在下面的代码中展开 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-10-24 21:20:46

问题是,如果将 ColumnWidth 设置为 Auto,则将根据内容(此处为 dataGrid)计算列宽。因此,您的第二列将非常大(即使您可能看不到它,具体取决于您的布局),并且每次都会绘制 dataGrid 的列。

所以基本上,您失去了columnVirtualization(而不是rowVirtualization)的好处。

这与您永远不应将 DataGrid 放入 ScrollViewer 的原因相同。

解决方案
在网格上设置 VirtualizingStackPanel.IsVirtualizing="True"

如果这不起作用,您可能需要自己调整大小,没有真正的选择。

The problem is that if you set the ColumnWidth to Auto, 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 (not rowVirtualization).

This is the same reason why you should never put a DataGrid into a ScrollViewer.

Solution
Set VirtualizingStackPanel.IsVirtualizing="True" on your grid

if this does not work, you might have to take care of the resizing yourself, no real option there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文