我正在尝试删除焦点节点以及来自TreeList
的所有子节点(如果有)。问题是我无法检索子节点。我尝试了两种方法:
我使用TreeListNodeIterator
返回一个TreeListNodes
对象,其中包含焦点行的子节点:
Public Class TreeListOperationGetChildNodes
Inherits TreeListOperation
Private _child_nodes As TreeListNodes
Public Sub New(tree As TreeList)
_child_nodes = New TreeListNodes(tree)
End Sub
Public Overrides Sub Execute(node As DevExpress.XtraTreeList.Nodes.TreeListNode)
_child_nodes.Add(node)
End Sub
Public ReadOnly Property ChildNodes() As TreeListNodes
Get
Return _child_nodes
End Get
End Property
End Class
然后,使用此child_nodes
对象删除TreeList
中的所有节点:
Dim child_nodes As TreeListNodes = New TreeListNodes(DataTreeList)
Dim op As New TreeListOperationGetChildNodes(DataTreeList)
Dim nodes As TreeListNodes = New TreeListNodes(DataTreeList)
DataTreeList.NodesIterator.DoLocalOperation(op, nodes)
child_nodes = op.ChildNodes
For Each node As TreeListNode In child_nodes
DataTreeList.DeleteNode(node)
Next
我尝试的另一种方法是循环遍历树形图的所有节点,看看它们的parent_id
是否等于聚焦节点id
。如果是,则将它们放入堆栈。最后,为堆栈中的每个节点调用删除节点。
Dim nodeStack As New Stack(Of DevExpress.XtraTreeList.Nodes.TreeListNode)
nodeStack.Push(DataTreeList.FocusedNode)
FillStack(DataTreeList.FocusedNode, nodeStack)
For i As Integer = 0 To nodeStack.Count - 1 Step +1
DataTreeList.DeleteNode(nodeStack.Pop)
Next
Private Sub FillStack(ByVal node As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal nodestack As Stack(Of DevExpress.XtraTreeList.Nodes.TreeListNode))
For Each childnode As DevExpress.XtraTreeList.Nodes.TreeListNode In DataTreeList.Nodes
If DirectCast(DataTreeList.GetDataRecordByNode(childnode), Data).ParentID = _
DirectCast(DataTreeList.GetDataRecordByNode(node), Data).ID Then
nodestack.Push(childnode)
FillStack(childnode, nodestack)
End If
Next
End Sub
上述代码段的问题:
chlid_nodes
为空(未找到子节点)FillDeleteStack
DataTreeList.Nodes
中只包含2个仅为父节点的节点。所以堆栈总是空的。我无法理解为什么我无法遍历子节点。为什么TreeList
中的节点集合只包含父节点而不包含子节点?
答案 0 :(得分:2)
0。要删除焦点节点及其所有子节点,您只需使用TreeList.DeleteNode
方法:
DataTreeList.DeleteNode(DataTreeList.FocusedNode)
1。您正在使用TreeListNodes(TreeList)
构造函数创建空节点集合。如果您想获取现有节点的集合,可以使用TreeList.Nodes
或TreeListNode.Nodes
属性:
nodes = DataTreeList.Nodes
' or
nodes = DataTreeList.FocusedNode.Nodes
2。 TreeList.Nodes
属性返回TreeList
的 root 节点的集合。如果您想在TreeList
中获取所有可见节点,则可以使用TreeList.GetNodeList
方法。