Expand TreeViewItems in wpf fast
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 th开发者_如何学Pythonat 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.
Take a look at the posts made by Bea Stollnitz about treeviews and performance.
Relevant post would be:
- How can I expand items in a TreeView? Part I / Part II / Part III
- Are there any tricks that will help me improve TreeView’s performance? Part I / Part II / Part III
She does a good job explaining in detail all of the options you can try out. Too much content to put it all here.
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...
精彩评论