1

I know how to expand all nodes from a treeview:

<TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="TreeViewItem.IsExpanded" Value="True"/>
            </Style>
 </TreeView.ItemContainerStyle>

the only problem is that it takes about a minute to expand all nodes... that's probably because there are so many items. How could I speed up this process?


Edit:

So I have a list: List<ScanItems> MyFilesList

ScanItem is a class that has properties such as: FullName, Name, Size, DateCreated, ComparePath and other specific properties that I need that's why I did not used the FileInfo class.

ScanFile is a class that inherits from ScanItems so it is just like it with the addition of other custom properties.

ScanDir is another class that inherits also from ScanItem and it has the following property:

public List<ScanItem> items{get;set;}  

the reason why I have included that property is so that I can have another list withing an item.

Look at this question regarding how to populate a treeview from a list of files.

so now I hope I explain my self correctly on how I am binding that list to the treeview.

Now let me explain how I added the files to MyFilesList. I created a recursion method to look for files in a directory. If the curent directory contained a file then add a ScanFile item. If it contained a Folder then add a ScanDir object and call the same method again passing the list of ScanDir. So this process takes about 8 seconds to scan my external hard drive. after that method get executed my list may contain just 4 items but one of those items will contain a list of maybe 20 items and so forth just like a folder may have 5 items and if one of those 5 items happens to be a folder that folder can have additional items.

So when I execute TreeView.DataContext = MyFilesList the treeview gets populated in less than a second. But when I include:

<TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="TreeViewItem.IsExpanded" Value="True"/>
            </Style>
 </TreeView.ItemContainerStyle>

that style inside the treeview the treeview takes to long to load.

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • How many are we talking about? If you're showing data from a database using Linq then maybe have a look what is your linq doing - maybe you're hitting the database for each record separately. – hyp Jul 06 '11 at 07:57
  • The list is binded to the treeview. the treeview consist of about 60,000 items. When I exclude the style from the project it does not take time to load. – Tono Nam Jul 07 '11 at 16:14
  • 1
    So that is a lot but how are you retrieving the items from the database? The code would be good. If you're using linq with a navigation property "Child" (or similar) then when you're expanding a node then most probably you're hitting the database for it... about 60k times – hyp Jul 07 '11 at 21:20
  • Hyp thanks a lot for the help you are right I am not being clear how I populated my list. Take a look at my edit. I am working on it now... – Tono Nam Jul 07 '11 at 21:50

2 Answers2

3

Take a look at the posts made by Bea Stollnitz about treeviews and performance.
Relevant post would be:

She does a good job explaining in detail all of the options you can try out. Too much content to put it all here.

Sevenate
  • 6,221
  • 3
  • 49
  • 75
RoelF
  • 7,483
  • 5
  • 44
  • 67
2

Have you tried looping through the treeviewitems and expanding them "manually" by setting

IsExpanded = true;

If that doesn't work then try a work around and add to your ScanDir ( I presume that's the only class which you expand? ) a property IsExpanded (or similar) and bind to it in your template. Not the best of solutions practise wise but if it would work...

hyp
  • 1,370
  • 1
  • 9
  • 21