c# - 当非虚拟化 DataGrid 中有大量行时,WPF 应用程序 DataGrid 控制窗口切换滞后

标签 c# .net wpf datagrid wpfdatagrid

我们有一个小型 .NET 4.0 DataGrid WPF 演示,代码如下。它由具有 30 列和 3000 行的非虚拟化 DataGrid 组成。它是非虚拟化的,因为我们需要不支持虚拟化的分组功能。

当您运行此应用并在它与其他窗口之间切换时,会出现明显的滞后(大约 1 秒)。这仅在窗口重新激活时发生 - 一旦激活,在内部点击没有相关延迟。

我们已经使用性能分析器分析了窗口重新激活时发生的这种滞后,并发现当窗口重新获得焦点时会触发大量依赖属性通知。我们不知道为什么会这样,而且似乎没有必要。

我们发现此延迟与 DataGrid 中的行数成正比。有谁知道我们如何消除或减少这种滞后?

更新:即使停留在应用程序内但关注另一个控件(例如网格外的文本框)时,似乎也会出现焦点滞后。因此我们现在知道这不是窗口切换问题,而是焦点变化引起的问题,但仍不确定确切原因。

(主窗口.xaml)

<Window x:Class="WpfApplication20.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="dataGrid" VirtualizingStackPanel.IsVirtualizing="False" AutoGenerateColumns="True"/>
    </Grid>
</Window>

(主窗口.xaml.cs)

using System.Collections.Generic;
using System.Windows;

namespace WpfApplication20
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<Row> rows = new List<Row>();

            for(int i = 0; i < 3000; i++)
            {
                Row row = new Row(i);
                rows.Add(row);
            }

            dataGrid.ItemsSource = rows;
        }
    }

    public class Row
    {
        public double Double1 { get; set; }
        public double Double2 { get; set; }
        public double Double3 { get; set; }
        public double Double4 { get; set; }
        public double Double5 { get; set; }
        public double Double6 { get; set; }
        public double Double7 { get; set; }
        public double Double8 { get; set; }
        public double Double9 { get; set; }
        public double Double10 { get; set; }
        public double Double11 { get; set; }
        public double Double12 { get; set; }
        public double Double13 { get; set; }
        public double Double14 { get; set; }
        public double Double15 { get; set; }
        public double Double16 { get; set; }
        public double Double17{ get; set; }
        public double Double18 { get; set; }
        public double Double19 { get; set; }
        public double Double20 { get; set; }
        public double Double21 { get; set; }
        public double Double22 { get; set; }
        public double Double23 { get; set; }
        public double Double24 { get; set; }
        public double Double25 { get; set; }
        public double Double26 { get; set; }
        public double Double27 { get; set; }
        public double Double28 { get; set; }
        public double Double29 { get; set; }
        public double Double30 { get; set; }

        public Row(double d)
        {
            Double1 = d;
            Double2 = d + 1;
            Double3 = d + 2;
            Double4  = d + 3;
            Double5  = d + 4;
            Double6  = d + 5;
            Double7  = d + 6;
            Double8  = d + 7;
            Double9  = d + 8;
            Double10 = d + 9;
            Double11 = d + 10;
            Double12 = d + 11;
            Double13 = d + 12;
            Double14 = d + 13;
            Double15 = d + 14;
            Double16 = d + 15;
            Double17 = d + 16;
            Double18 = d + 17;
            Double19 = d + 18;
            Double20 = d + 19;
            Double21 = d + 20;
            Double22 = d + 21;
            Double23 = d + 22;
            Double24 = d + 23;
            Double25 = d + 24;
            Double26 = d + 25;
            Double27 = d + 26;
            Double28 = d + 27;
            Double29 = d + 28;
            Double30 = d + 29;
        }             
    }                 
}

(组样式 - 可选择通过放入 DataGrid XML 启用):

<!--<DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Border BorderBrush="DarkGray" BorderThickness="1" Padding="4,0" >
                                            <Expander VerticalContentAlignment="Center" IsExpanded="True">
                                                <Expander.Header>
                                                    <Canvas>
                                                        <StackPanel Orientation="Horizontal" Canvas.Top="-11" Canvas.Left="4">
                                                            <Label Content="{Binding Name}" Visibility="{Binding DataContext.ShowGroupHeaderVisibility, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                                                            <Label Content="{Binding ItemCount}" Visibility="{Binding DataContext.ShowGroupCountVisibility, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                                                        </StackPanel>
                                                    </Canvas>
                                                </Expander.Header>
                                                <ItemsPresenter/>
                                            </Expander>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>-->

最佳答案

我会尝试在行类(或 rowViewModel 类)中使用 Dependecy 属性而不是常规 CLR 属性,尤其是因为有这么多列。

您是否使用任何样式/模板?我假设您发布的代码有所简化 :) 另外,你是说当窗口打开时你没有看到这种延迟?只有当它重新成为焦点时?

-编辑-

好吧,我做了一些测试并得出结论,绑定(bind)不是问题所在。我似乎确实能够通过定义自定义单元格样式(并将其设置为共享)来降低窗口获得焦点时的 cpu 峰值:

<Window>
  <Grid>
    <Grid.Resources>
      <Style TargetType="DataGridCell"
             x:Key="cs"
             x:Shared="True">
        <Setter Property="Content"
                Value="{Binding}" />
      </Style>
    </Grid.Resources>
    <DataGrid Name="dataGrid"
              VirtualizingStackPanel.IsVirtualizing="False"
              CellStyle="{StaticResource cs}">
    </DataGrid>
  </Grid>
</Window>

尽管如此,该程序仍使用大量内存,几乎 1 GB.. 此外,由于某种原因,在我的机器上横向滚动速度很慢,但向下滚动没问题。我不认为内置的数据网格真的适合这种数据量,至少在非虚拟化模式下。您仍然可以尝试单元格样式,看看是否可以改进

关于c# - 当非虚拟化 DataGrid 中有大量行时,WPF 应用程序 DataGrid 控制窗口切换滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6451616/

相关文章:

c# - 编译错误集合被修改

c# - 在 WPF 应用程序中按下 Return 时如何模拟 Tab 键按下?

c# - 将 Redis 与 SignalR 结合使用

c# - 表单发布时获取 HTTP ERROR 400

.net - 在.Net 2.0中将8位灰度tiff图像转换为8位灰度jpg

c# - 延迟任务开始的正确方法

c# - 以样式更改按钮的内容?

wpf - 具有DataTemplate的Window.Content

c# - Visual Studio 2017 - 悬停指针时更改工具提示

c# - 如何在 MonoDevelop 中为旧版本指定添加依赖项?