如何确定所选节点是 TreeView 中的子节点还是父节点?
如何确定所选节点是 TreeView
控件中的子节点还是父节点?
How can I find out if the selected node is a child node or a parent node in the TreeView
control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
具体如何实现此类检查取决于您如何定义“子”和“父”节点。但每个
TreeNode
对象公开了两个属性,它们提供了重要信息:Nodes
属性 返回该特定节点包含的TreeNode
对象的集合。因此,只需检查所选节点包含多少个子节点,就可以确定它是否是父节点:要获取更多信息,您还可以检查
Parent属性。如果此值为
null
,则该节点位于TreeView
的根级别(它没有父级):Exactly how you implement such a check depends on how you define "child" and "parent" nodes. But there are two properties exposed by each
TreeNode
object that provide important information:The
Nodes
property returns the collection ofTreeNode
objects contained by that particular node. So, by simply checking to see how many child nodes the selected node contains, you can determine whether or not it is a parent node:To obtain more information, you can also examine the value of the
Parent
property. If this value isnull
, then the node is at the root level of theTreeView
(it does not have a parent):您可以使用
TreeNode .Parent
属性。如果其值为
null
引用,则该节点位于根级别。You can use the
TreeNode.Parent
property for this.If its value is a
null
-reference, the node is at the root level.试试这个
Try this
对于根节点是父 TreeView .. 可以检查我们是否比较 -> 的类型
For root node is the parent TreeView .. it is possible to check if we compare the types of ->
是最好的选择。
is the best to choose.