TreeNodeCollection TreeNodeCollection TreeNodeCollection TreeNodeCollection Class

定义

表示 TreeNode 控件中 TreeView 对象的集合。Represents a collection of TreeNode objects in the TreeView control. 此类不能被继承。This class cannot be inherited.

public ref class TreeNodeCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class TreeNodeCollection : System.Collections.ICollection, System.Web.UI.IStateManager
type TreeNodeCollection = class
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public NotInheritable Class TreeNodeCollection
Implements ICollection, IStateManager
继承
TreeNodeCollectionTreeNodeCollectionTreeNodeCollectionTreeNodeCollection
实现

示例

下面的示例演示如何以编程方式将节点添加到中, 并TreeNodeCollection将其从中移除。The following example demonstrates how to programmatically add nodes to and remove them from a TreeNodeCollection. 请注意, ChildNodes TreeNodeCollection和属性返回对象。 NodesNotice that the Nodes and ChildNodes properties return a TreeNodeCollection object.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    if (!IsPostBack)
    {

      // Use the Add and Remove methods to programmatically 
      // remove the Appendix C node and replace it with a new 
      // node. 
      LinksTreeView.Nodes.Remove(LinksTreeView.Nodes[3]);
      LinksTreeView.Nodes.Add(new TreeNode("New Appendix C"));

      // Use the AddAt and RemoveAt methods to programmatically 
      // remove the Chapter One node and replace it with a new node.
      LinksTreeView.Nodes[0].ChildNodes.RemoveAt(0);
      LinksTreeView.Nodes[0].ChildNodes.AddAt(0, new TreeNode("New Chapter One"));

      // Use the Clear method to remove all the child nodes of the 
      // Chapter Two node.
      LinksTreeView.Nodes[0].ChildNodes[1].ChildNodes.Clear();

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        runat="server">
         
        <LevelStyles>
        
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Bold="true" 
            Font-Size="12pt" 
            ForeColor="DarkGreen"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-Bold="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-UnderLine="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Size="8pt"/>
             
        </LevelStyles>
         
        <Nodes>
        
          <asp:TreeNode Text="Table of Contents"
            Expanded="true">
             
            <asp:TreeNode Text="Chapter One">
            
              <asp:TreeNode Text="Section 1.0">
              
                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>
              
              </asp:TreeNode>
              
              <asp:TreeNode Text="Section 1.1">
              
                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
            <asp:TreeNode Text="Chapter Two">
            
              <asp:TreeNode Text="Section 2.0">
              
                <asp:TreeNode Text="Topic 2.0.1"/>
                <asp:TreeNode Text="Topic 2.0.2"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
          
          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />
        
        </Nodes>
        
      </asp:TreeView>

    </form>
  </body>
</html>


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    If Not IsPostBack Then

      ' Use the Add and Remove methods to programmatically 
      ' remove the Appendix C node and replace it with a new 
      ' node. 
      LinksTreeView.Nodes.Remove(LinksTreeView.Nodes(3))
      LinksTreeView.Nodes.Add(New TreeNode("New Appendix C"))

      ' Use the AddAt and RemoveAt methods to programmatically 
      ' remove the Chapter One node and replace it with a new node.
      LinksTreeView.Nodes(0).ChildNodes.RemoveAt(0)
      LinksTreeView.Nodes(0).ChildNodes.AddAt(0, New TreeNode("New Chapter One"))

      ' Use the Clear method to remove all the child nodes of the 
      ' Chapter Two node.
      LinksTreeView.Nodes(0).ChildNodes(1).ChildNodes.Clear()

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        runat="server">
         
        <LevelStyles>
        
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Bold="true" 
            Font-Size="12pt" 
            ForeColor="DarkGreen"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-Bold="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-UnderLine="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Size="8pt"/>
             
        </LevelStyles>
         
        <Nodes>
        
          <asp:TreeNode Text="Table of Contents"
            Expanded="true">
             
            <asp:TreeNode Text="Chapter One">
            
              <asp:TreeNode Text="Section 1.0">
              
                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>
              
              </asp:TreeNode>
              
              <asp:TreeNode Text="Section 1.1">
              
                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
            <asp:TreeNode Text="Chapter Two">
            
              <asp:TreeNode Text="Section 2.0">
              
                <asp:TreeNode Text="Topic 2.0.1"/>
                <asp:TreeNode Text="Topic 2.0.2"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
          
          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />
        
        </Nodes>
        
      </asp:TreeView>

    </form>
  </body>
</html>

注解

类用于存储和管理TreeView控件中对象的TreeNode集合。 TreeNodeCollectionThe TreeNodeCollection class is used to store and manage a collection of TreeNode objects in the TreeView control. 控件在其两TreeNodeCollection个属性中使用类。 TreeViewThe TreeView control uses the TreeNodeCollection class in two of its properties. 它将其根节点存储在Nodes属性中, 并将其所CheckedNodes选节点存储在属性中。It stores its root nodes in the Nodes property and its selected nodes in the CheckedNodes property. 集合还用于存储子节点ChildNodes的属性 (如果有)。 TreeNodeCollectionThe TreeNodeCollection collection is also used for the ChildNodes property to store child nodes (if any).

TreeNodeCollection类支持多种方法来访问集合中的项:The TreeNodeCollection class supports several ways to access the items in the collection:

  • 使用索引器在特定的TreeNode从零开始的索引处直接检索对象。 Item[Int32]Use the Item[Int32] indexer to retrieve a TreeNode object directly at a specific zero-based index.

  • GetEnumerator使用方法可创建一个可用于循环访问集合的枚举器。Use the GetEnumerator method to create an enumerator that can be used to iterate through the collection.

  • 使用foreach (C#) 或For Each (Visual Basic) 来循环访问集合。Use foreach (C#) or For Each (Visual Basic) to iterate through the collection.

  • 使用方法可将集合的内容复制Array到对象中。 CopyToUse the CopyTo method to copy the contents of the collection into an Array object.

可以TreeNodeCollection通过添加和删除TreeNode对象以编程方式管理。You can programmatically manage the TreeNodeCollection by adding and removing TreeNode objects. 若要向集合添加节点, 请使用AddAddAt方法。To add nodes to the collection, use the Add or AddAt method. 若要从集合中删除节点, 请Remove使用RemoveAt、或Clear方法。To remove nodes from the collection, use the Remove, RemoveAt, or Clear method.

备注

将控件绑定到数据源后, 每次发生Nodes绑定ChildNodes时, 将自动填充和集合。 TreeViewWhen the TreeView control is bound to a data source, the Nodes and ChildNodes collections are automatically populated each time binding occurs. 绑定之间对集合所做的任何更改都将丢失。Any changes to the collections between bindings will be lost. 若要保留这些更改, 请更新数据源, 或者在每次绑定时手动重新生成集合。To retain these changes, either update the data source or manually rebuild the collection each time you bind.

TreeNodeCollection包含的属性和方法可用于检索有关集合本身的信息。The TreeNodeCollection contains properties and methods that allow you to retrieve information about the collection itself. 若要确定集合中有多少项, 请使用Count属性。To find out how many items are in the collection, use the Count property. 如果要确定集合是否包含TreeNode某个对象, 请Contains使用方法。If you want to determine whether the collection contains a certain TreeNode object, use the Contains method. 若要获取集合中TreeNode对象的索引, 请IndexOf使用方法。To get the index of a TreeNode object in the collection, use the IndexOf method.

构造函数

TreeNodeCollection() TreeNodeCollection() TreeNodeCollection() TreeNodeCollection()

用默认值初始化 TreeNodeCollection 类的新实例。Initializes a new instance of the TreeNodeCollection class using the default values.

TreeNodeCollection(TreeNode) TreeNodeCollection(TreeNode) TreeNodeCollection(TreeNode) TreeNodeCollection(TreeNode)

使用指定的父节点(或所有者)初始化 TreeNodeCollection 类的新实例。Initializes a new instance of the TreeNodeCollection class using the specified parent node (or owner).

属性

Count Count Count Count

获取 TreeNodeCollection 对象中的项数。Gets the number of items in the TreeNodeCollection object.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

获取一个值,该值指示是否同步对 TreeNodeCollection 的访问(线程安全)。Gets a value indicating whether access to the TreeNodeCollection is synchronized (thread safe).

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

获取 TreeNode 对象中指定索引处的 TreeNodeCollection 对象。Gets the TreeNode object at the specified index in the TreeNodeCollection object.

SyncRoot SyncRoot SyncRoot SyncRoot

获取一个对象,该对象可用于同步对 TreeNodeCollection 对象的访问。Gets an object that can be used to synchronize access to the TreeNodeCollection object.

方法

Add(TreeNode) Add(TreeNode) Add(TreeNode) Add(TreeNode)

将指定的 TreeNode 对象追加到 TreeNodeCollection 对象的结尾。Appends the specified TreeNode object to the end of the TreeNodeCollection object.

AddAt(Int32, TreeNode) AddAt(Int32, TreeNode) AddAt(Int32, TreeNode) AddAt(Int32, TreeNode)

将指定的 TreeNode 对象插入到 TreeNodeCollection 对象中的指定索引位置。Inserts the specified TreeNode object in a TreeNodeCollection object at the specified index location.

Clear() Clear() Clear() Clear()

清空 TreeNodeCollection 对象。Empties the TreeNodeCollection object.

Contains(TreeNode) Contains(TreeNode) Contains(TreeNode) Contains(TreeNode)

确定指定的 TreeNode 对象是否在集合中。Determines whether the specified TreeNode object is in the collection.

CopyTo(TreeNode[], Int32) CopyTo(TreeNode[], Int32) CopyTo(TreeNode[], Int32) CopyTo(TreeNode[], Int32)

从目标数组的指定索引开始,将 TreeNodeCollection 对象中的所有项复制到 TreeNode 对象的一维兼容数组。Copies all the items from the TreeNodeCollection object to a compatible one-dimensional array of TreeNode objects, starting at the specified index in the target array.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

返回一个可用于循环访问 TreeNodeCollection 对象的枚举数。Returns an enumerator that can be used to iterate through a TreeNodeCollection object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IndexOf(TreeNode) IndexOf(TreeNode) IndexOf(TreeNode) IndexOf(TreeNode)

确定指定的 TreeNode 对象的索引。Determines the index of the specified TreeNode object.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(TreeNode) Remove(TreeNode) Remove(TreeNode) Remove(TreeNode)

TreeNode 对象中移除指定的 TreeNodeCollection 对象。Removes the specified TreeNode object from the TreeNodeCollection object.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

TreeNode 对象中移除指定索引位置的 TreeNodeCollection 对象。Removes the TreeNode object at the specified index location from the TreeNodeCollection object.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

从目标数组的指定索引开始,将 TreeNodeCollection 对象中的所有项复制到兼容的一维 Array 中。Copies all the items from the TreeNodeCollection object to a compatible one-dimensional Array, starting at the specified index in the target array.

IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState

获取一个值,该值指示 TreeNodeCollection 对象是否保存对其视图状态的更改。Gets a value indicating whether the TreeNodeCollection object is saving changes to its view state.

IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object)

加载 TreeNodeCollection 对象先前保存的视图状态。Loads the TreeNodeCollection object's previously saved view state.

IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState()

将视图状态的更改保存到 ObjectSaves the changes to view state to a Object.

IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState()

指示 TreeNodeCollection 跟踪其视图状态的更改。Instructs the TreeNodeCollection to track changes to its view state.

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅