TreeView slower than the Winforms version
- 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.
 
**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.

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
Assessment
This issue has not been assessed yet.