在wpf中快速展开TreeViewItems

发布于 2024-11-18 18:09:50 字数 1629 浏览 4 评论 0原文

我知道如何从树视图展开所有节点:

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

唯一的问题是展开所有节点大约需要一分钟......这可能是因为有太多项目。我怎样才能加快这个过程?


编辑:

所以我有一个列表: List; MyFilesList

ScanItem 是一个类,它具有以下属性:FullName、Name、Size、DateCreated、ComparePath 以及我需要的其他特定属性,这就是我没有使用 FileInfo 类的原因。

ScanFile 是一个继承自 ScanItems 的类,因此它与 ScanItems 类似,但添加了其他自定义属性。

ScanDir 是另一个也继承自 ScanItem 的类,它具有以下属性:

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

我包含该属性的原因是这样我可以在一个项目中拥有另一个列表。

查看这个问题了解如何从文件列表填充树视图。

所以现在我希望我能正确地解释我如何将该列表绑定到树视图。

现在让我解释一下如何将文件添加到 MyFilesList。我创建了一个递归方法来查找目录中的文件。如果当前目录包含文件,则添加 ScanFile 项。如果它包含一个文件夹,则添加一个 ScanDir 对象,并再次调用相同的方法传递 ScanDir 列表。所以这个过程大约需要 8 秒来扫描我的外部硬盘。执行该方法后,我的列表可能只包含 4 个项目,但其中一个项目将包含可能包含 20 个项目的列表,依此类推,就像一个文件夹可能有 5 个项目,如果这 5 个项目之一恰好是一个文件夹,则该文件夹可以有额外的项目。

因此,当我执行 TreeView.DataContext = MyFilesList 时,树视图将在不到一秒的时间内填充。但是当我在树视图中包含:

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

该样式时,树视图需要很长时间才能加载。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思碎 2024-11-25 18:09:50

查看 Bea Stollnitz 发表的关于树视图和性能的帖子。
相关帖子是:

她详细解释得很好您可以尝试的所有选项。内容太多,无法全部放在这里。

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.

守护在此方 2024-11-25 18:09:50

您是否尝试过循环遍历树视图项目并通过设置“手动”扩展它们

IsExpanded = true;

如果这不起作用,则尝试解决方法并添加到您的 ScanDir (我认为这是您扩展的唯一类?)属性 IsExpanded (或类似属性)并在模板中绑定到它。实践中并不是最好的解决方案,但如果它有效的话......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文