dotnet / dotnet/wpf

TreeView slower than the Winforms version

Open
#6,407 2 comments 0 reactions 0 assignees View on GitHub
help wanted Performance
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: `6.0.200`
* Windows version: `22H2 (OS Build 22593.1)`
* Does the bug reproduce also in WPF for .NET Framework 4.8?
* Sorry, I don't have that option for some reason :(. And yes I have it installed.
![image](https://user-images.githubusercontent.com/26075577/163089142-b5df4b44-5310-4f28-8fc8-93e3a55698c0.png) ![image](https://user-images.githubusercontent.com/26075577/163089270-b0e9c3b9-5a74-4a2c-8ca5-56833655f89a.png)

**Problem description:**
When expanding nodes, the WPF `TreeView` takes longer to load than the WinForms one does.

**Minimal repro:**
I've attached a very small project for reproducing this.
[TreeViewComparison.zip](https://github.com/dotnet/wpf/files/8477829/TreeViewComparison.zip)
On the left is a WPF `TreeView`, on the right is a WinForms `TreeView` wrapped in `WindowsFormsHost`. Both are prepopulated with an identical bunch of nested nodes. Click the WPF text to run `root.ExpandSubtree()`, and click the WinForms text to run `root.ExpandAll()`. Click the text, not the expand arrows, since that will just open one layer.
![image](https://user-images.githubusercontent.com/26075577/163090457-5f3c5a5c-4776-4a09-a3c7-648a95af753e.png)
Here I've copied the two relevant files from the solution:

MainWindow.xaml

```xaml












```

MainWindow.xaml.cs

```cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;

namespace TreeViewComparison;

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var wpf_root = new TreeViewItem() { Header = "Click to expand WPF nodes" };
var win_root = new TreeNode("Click to expand WinForms nodes");
WPFTree.Items.Add(wpf_root);
WinFormsTree.Nodes.Add(win_root);
AddGenerations(wpf_root, 5, 5);
AddGenerations(win_root, 5, 5);
wpf_root.Selected += (s, e) => wpf_root.ExpandSubtree();
WinFormsTree.AfterSelect += (s, e) => win_root.ExpandAll();
}

private void AddGenerations(TreeViewItem item, int children, int generations)
{
for (int i = 0; i < children; i++)
{
var child = new TreeViewItem() { Header = $"Child #{i + 1}" };
item.Items.Add(child);
if (generations > 0)
AddGenerations(child, children, generations - 1);
}
}

private void AddGenerations(TreeNode item, int children, int generations)
{
for (int i = 0; i < children; i++)
{
var child = new TreeNode($"Child #{i + 1}");
item.Nodes.Add(child);
if (generations > 0)
AddGenerations(child, children, generations - 1);
}
}
}
```

**Expected behavior:**
WPF should be as fast as, if not faster than, WinForms at loading `TreeView` nodes.

**Actual behavior:**
The WPF `TreeView` takes significantly longer to finish. On my computer, the WinForms view fully expands in about 8 seconds, while the WPF view takes a staggering 62 seconds. Honestly, both of these are unacceptably slow.

---

I am new to this repository and WPF in general so please let me know if I did anything wrong, and thank you for your work! :)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.