1

我正在尝试删除焦点节点及其所有子节点(如果有)TreeList。问题是我无法检索子节点。我尝试了两种方法:

  1. 我使用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
  1. 我尝试的另一种方法是循环遍历树列表的所有节点,看看它们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
    

上述代码片段的问题:

  1. 这里节点迭代器返回chlid_nodes空(未找到子节点)
  2. InFillDeleteStack DataTreeList.Nodes仅包含 2 个仅作为父节点的节点。所以堆栈总是空的。

我不明白为什么我不能遍历子节点。为什么节点集合TreeList只包含父节点而不包含子节点?

4

1 回答 1

2

0. 要删除焦点节点及其所有子节点,您可以简单地使用TreeList.DeleteNode方法:

DataTreeList.DeleteNode(DataTreeList.FocusedNode)

1.您正在使用TreeListNodes(TreeList)创建一个节点集合的构造函数。如果要获取现有节点的集合,则可以使用TreeList.NodesorTreeListNode.Nodes属性:

nodes = DataTreeList.Nodes
' or
nodes = DataTreeList.FocusedNode.Nodes

2.TreeList.Nodes属性返回 的TreeList根节点集合。如果要获取所有可见节点,TreeList则可以使用TreeList.GetNodeList方法。

于 2014-11-13T05:41:53.287 回答