Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TreeView optimization with large amount of data

I have a treeview that I put ~4000 objects in. The initial load and fill of my treeview comes from a list of objects, and it takes a LONG time. This is how I fill it :

private List<ItemIdPair> m_itemList;
public List<ItemIdPair> ItemList {
    get { return m_itemList; }
    set { m_itemList = value; }
}

public void Window_Loaded(object sender, RoutedEventArgs e) {
    try {
        ItemList = ItemListParse(); // data from .txt file (NOT the performance problem)
        ItemList = ItemList.OrderBy(o => o.ItemName).ToList();
        ItemTreeView.DataContext = ItemList;
    } catch(Exception ex) { }
    }

My treeview is bound to my Datacontext in the XAML.

I also have a textbox to search the treeview and narrow the results which ALSO has a huge performance hit. It looks like this :

public void LoadTree(string search) {
        try {
            List<ItemIdPair> items = ItemList.Where(i => i.ItemName.ToLower().Contains(search.ToLower())).ToList();
            ItemTreeView.DataContext = items;
        } catch(Exception ex) { }
    }

any tips on how I can optimize this, or refactor it to give me a performance boost? There's no way that a Treeview should behave this slow.

like image 686
Jason Higgins Avatar asked Jun 08 '26 09:06

Jason Higgins


1 Answers

Try adding Virtualization to your TreeView.

<TreeView Name="MyTreeView" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" />
like image 130
Matt Densmore-Crew Avatar answered Jun 11 '26 02:06

Matt Densmore-Crew