树视图刷新问题
我有一个用户无法编辑的树视图。我尝试刷新的方式是清除整个树并重新添加所有节点(和子节点)。我通过以下代码行来完成此操作:
treeView.BeginUpdate(); //Freeze drawing
treeView.Nodes.Clear(); //Empty Tree
addAllNodes(); //This adds the nodes for the tree and sets their name/text property
treeView.EndUpdate(); //Unfreeze drawing
我尝试在调用 addAllNodes 之前添加 Update 和 Refresh 方法,但没有产生任何影响。执行上述操作会出现错误:
System.ArgumentException:无法在多个位置添加或插入项目“NodeNameHere”。您必须首先将其从当前位置删除或克隆它
我的第一个问题是,我做了什么会导致此错误以及如何正确刷新我的树?
我的第二个问题是,刷新后有什么办法可以恢复用户扩展的节点吗? (这样一切就不会崩溃)
I have a Treeview that the user cannot edit. The way I'm trying to refresh is clearing the entire tree and re-adding all the nodes (and children). I'm accomplishing this by the following lines of code:
treeView.BeginUpdate(); //Freeze drawing
treeView.Nodes.Clear(); //Empty Tree
addAllNodes(); //This adds the nodes for the tree and sets their name/text property
treeView.EndUpdate(); //Unfreeze drawing
I've tried adding the Update and Refresh method before I call addAllNodes but hasn't made a difference. Doing the above gets me an error:
System.ArgumentException: Cannot add or insert the item 'NodeNameHere' in more than one place. You must first remove it from its current location or clone it
My first question is, what am I doing to cause this error and how can I properly refresh my tree?
My second question is, after the refresh is there any way I can restore the user's expanded nodes? (so that everything does not end up collapsed)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个节点都有一个路径(称为 .Path 或 .FullPath 之类的路径;现在没有打开 VS)。因此,您可以在清除节点并记录所有展开的节点之前遍历节点,然后在刷新后再次遍历它们,看看节点的路径是否与存储的路径匹配。如果匹配,则展开它。
each node has a path (called something like .Path or .FullPath; dont have VS open right now). So you can walk your nodes before you clear them and record all the expanded nodes, then walk them again after refresh and see if the node's path matches a stored one.. if it does, expand it.