无法删除父节点的子节点(Devexpress TreeList)

时间:2014-11-12 12:26:05

标签: vb.net devexpress devexpress-windows-ui xtratreelist

我正在尝试删除焦点节点以及来自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
    
  2. 然后,使用此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
      
    2. 上述代码段的问题:

      1. 此处节点迭代器返回chlid_nodes为空(未找到子节点)
      2. FillDeleteStack DataTreeList.Nodes中只包含2个仅为父节点的节点。所以堆栈总是空的。
      3. 我无法理解为什么我无法遍历子节点。为什么TreeList中的节点集合只包含父节点而不包含子节点?

1 个答案:

答案 0 :(得分:2)

0。要删除焦点节点及其所有子节点,您只需使用TreeList.DeleteNode方法:

DataTreeList.DeleteNode(DataTreeList.FocusedNode)

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

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

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