Allon Guralnek
Allon Guralnek

Reputation: 16141

Poor performance when scrolling horizontally in a WPF4 DataGrid

I have an application that needs to display a grid of data consisting mostly of boolean values, and checkbox columns fit the bill. There will be about 40-60 checkbox columns with around 200 rows. I thought it would be trivial to do something like that in WPF.

I am unable to even get near a decent horizontal scrolling experience with WPF. To see what I'm talking about, create a Window and place a DataGrid on it, and set AutoGenerateColumns="True", then add the following in the constructor after InitializeComponent();:

dataGrid1.ItemsSource = Create(200, i => new { b1 = true, b2 = false, b3 = true, b4 = false, b5 = true, b6 = false, b7 = true, b8 = false, b9 = true,
                                               b10 = false, b11 = true, b12 = false, b13 = true, b14 = false, b15 = true, b16 = false, b17 = true, b18 = false, b19 = true, b20 = false, b21 = true,
                                               b22 = false, b23 = true, b24 = false, b25 = true, b26 = false, b27 = true, b28 = false, b29 = true, b30 = false, b31 = true, b32 = false });

It requires this method, add it too:

ObservableCollection<T> Create<T>(int count, Func<int, T> creator)
{
    return new ObservableCollection<T>(Enumerable.Range(0, count).Select(creator));
}

Run the application, and attempt to scroll horizontally. Horrible, isn't it? Vertical scrolling isn't too hot either.

I did the same with a WinForms application and scrolling was butter smooth in all directions, and looked as good as WPF after forcing the DataGridView's DoubleBuffered property to true. To compare, create a new Form, plop a DataGridView and add the following to the constructor after InitializeComponent();:

typeof(DataGridView).GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(dataGridView1, true, null);
for (int i = 0; i < 64; i++)
    dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn {HeaderText = "C" + i, Width = 30});
for (int i = 0; i < 400; i++)
    dataGridView1.Rows.Add();

This has twice the number of columns, twice the number of rows, and it scrolls as smooth as a newborn's bottom, both horizontally and vertically.

But unfortunately I can't do what I need with WinForms. I need templated columns. I need composability.

To solve the scrolling issue in WPF, I tried all combinations of the following:

I'm willing to accept subpar performance. I don't expect anything like the smoothness of WinForms (WPF is after all giving me so much more, although this should have been offset by WPF's GPU acceleration). I just want something usable, even just barely usable.

Upvotes: 2

Views: 2921

Answers (2)

paparazzo
paparazzo

Reputation: 45106

As stated in my comment I am not sure of the question. If you are looking for performance of a grid in WPF then go ListView / GridView. Unless there are some feature of DataGrid you have to have. It is more code but worth it. I have a GridView with 40 columns and 1000 rows and good performance (and DataGrid performance was bad).

Upvotes: 1

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26338

It's mainly because CheckBox visual tree is quite complicated. You can get a lot better performance, if you restyle the visual tree.

While having only single element in ControlTemplate(single border), there was no lag or delay. You can get specific style from here: http://msdn.microsoft.com/en-us/library/ms752319%28v=vs.85%29.aspx

There will be some time spent in databinding for sure. See this: Databinding optimization, it explains that binding to usual property takes longer. Usually because it involves reflection. When using dependency properties, there is no reflection involved. As far I know.

Your class is definitely not big, but you sure can test out. Will it perform better if everything is dependency property?

Let me know if you run into any issues.

Upvotes: 1

Related Questions