我正在尝试删除焦点节点及其所有子节点(如果有)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
空(未找到子节点) - In
FillDeleteStack
DataTreeList.Nodes
仅包含 2 个仅作为父节点的节点。所以堆栈总是空的。
我不明白为什么我不能遍历子节点。为什么节点集合TreeList
只包含父节点而不包含子节点?