We have a small .NET 4.0 DataGrid WPF demo with the code posted below. It consists of a non-virtualized DataGrid with 30 columns and 3000 rows. It is non-virtualized, due to us requiring grouping capabilities, which do not support virtualization.
When you run this app, and switch between it and other windows, there is a noticeable lag (of about 1 second). This only occurs when the window is reactivated - once activated, clicking around inside has no associated delay.
We have profiled this lag that occurs on window reactivation using the performance analyzer and have found that there are a lot of dependency property notifications being triggered when the window is brought back into focus. We do not know why this happens, and it seems unnecessary.
We find this delay is proportional to the number of rows in the DataGrid. Does anyone know how we can eliminate or reduce this lag?
UPDATE: It seems like the focus lag occurs even when staying inside the application but focusing on another control such as a textbox outside the grid. Therefore we now know it's not a Window switching problem, but one caused by change in focus, but are still unsure of the exact cause.
(MainWindow.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>
(MainWindow.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;
}
}
}
(Group Style - optionally enabled by putting inside 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>-->