【问题标题】:Loading 2 DataGrids + StackPanel with ScrollViewer Taking too long, Alternatives?使用 ScrollViewer 加载 2 个 DataGrids + StackPanel 时间太长,替代方案?
【发布时间】:2018-12-18 16:21:32
【问题描述】:

我有 3 个内容项,两个 DataGrid 和一个 StackPanel,它们都需要在 WPF 应用程序中一起滚动。只要我只有一个小数据集,ScrollViewer 一切正常,但是当我达到 500 行和 150 列时,它会在绘制屏幕之前滞后大约 8 秒。阅读此内容后:Unreasonable WPF DataGrid Loading Time 我禁用了ScrollViewer,它仅在 2.5 秒内加载完毕。

<ScrollViewer CanContentScroll="True"
              HorizontalScrollBarVisibility="Visible"
              VerticalScrollBarVisibility="Visible">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20" />
            <RowDefinition Height="22" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <DataGrid x:Name="DataGridViewHeader" CellEditEnding="ColumnNameUpdate" Grid.Row="0" HeadersVisibility="None" HorizontalAlignment="Stretch" Margin="0,0,0,0"  VerticalAlignment="Stretch"  ItemsSource="{Binding}" CurrentCellChanged="DataGridCellChange" Grid.Column="0" DataGridCell.Selected="DataGrid_GotFocus" />
            <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Name="ComboBoxPanel"  Grid.Row="1"  Margin="0,0,0,0"  HorizontalAlignment="Left" >

            </StackPanel>

        <DataGrid x:Name="DataGridView"  Grid.Row="2"    HeadersVisibility="None"    HorizontalAlignment="Stretch" Margin="0,0,0,0"  VerticalAlignment="Stretch"  ItemsSource="{Binding}" CurrentCellChanged="DataGridCellChange" Grid.Column="0"/>

    </Grid>
</ScrollViewer>

问题在于,ScrollViewer 提供了以下布局,它允许我重命名顶部的列,使用第二行的下拉菜单选择该列的 DataType,然后显示数据。有 150 个列,它们必须全部排成一列,但性能受到了巨大的打击。

虽然我知道ScrollViewer 是一个简单的解决方案,但考虑到性能,有没有更好的方法?

【问题讨论】:

  • 我的建议是在 Datagrid 上使用分页来显示至少 20 条记录。现在,如果用户需要搜索特定记录,您可以在网格上方提供一个文本过滤器,记录将立即显示在其中。
  • 这是一个很好的建议,但并不理想。我正在构建一个通用二进制数据库解析器youtube.com/watch?v=OMeghA82kSk,有时帮助您识别列和数据类型的记录在前 100 条左右的记录中不可见。我已经更新了我的 GUI,允许您使用滑块来选择要提取多少条记录,但如果可能的话,我仍然希望解决这个性能问题。
  • 合理的解决方案是在 ScrollViewer 中只有一个 DataGird 而不是所有 2 个 DataGrids+StackPanel。重新模板 DataGrid 标头,使每个标头都包含下拉列表
  • 感谢 ASh,这正是我想要的,但是当我在这里尝试时无法取得进展:stackoverflow.com/questions/53288632/…,这里:stackoverflow.com/questions/53272634/…,或这里:stackoverflow.com/questions/53215418/… 所以我最终结束了用这个方法

标签: wpf datagrid scrollview scrollbar


【解决方案1】:

移除 ScrollViewer 并添加

   VirtualizingStackPanel.IsVirtualizing="True" 
                VirtualizingStackPanel.VirtualizationMode = "recycling"
                EnableColumnVirtualization = "true"
                EnableRowVirtualization = "true"
                  VirtualizingPanel.IsVirtualizingWhenGrouping="True"
                MaxWidth="2560" MaxHeight="1600"

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多