TreeNodeCollection 類別

定義

代表 TreeNode 控制項中 TreeView 物件的集合。 此類別無法獲得繼承。

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
繼承
TreeNodeCollection
實作

範例

下列範例示範如何以程式設計方式將節點新增至 ,並從 中移除節點 TreeNodeCollection 。 請注意, 和 屬性會 Nodes 傳回 TreeNodeCollection 物件。 ChildNodes


<%@ 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>

備註

類別 TreeNodeCollection 是用來儲存和管理 控制項中的 TreeView 物件集合 TreeNode 。 控制項 TreeView 會在其 TreeNodeCollection 兩個屬性中使用 類別。 它會將其根節點儲存在 屬性中 Nodes ,並將其選取的節點儲存在 屬性中 CheckedNodes 。 如果有任何) ,集合 TreeNodeCollection 也會用於 ChildNodes 屬性來儲存子節點 (。

類別 TreeNodeCollection 支援數種方式來存取集合中的專案:

  • Item[]使用索引子直接擷取 TreeNode 特定以零起始的索引處的物件。

  • GetEnumerator使用 方法來建立可用來逐一查看集合的列舉值。

  • 使用 foreach (C#) 或 For Each (Visual Basic) 逐一查看集合。

  • CopyTo使用 方法,將集合 Array 的內容複寫到 物件中。

您可以藉由新增和移除 TreeNode 物件,以程式設計方式管理 TreeNodeCollection 。 若要將節點新增至集合,請使用 AddAddAt 方法。 若要從集合中移除節點,請使用 RemoveRemoveAtClear 方法。

注意

TreeView當控制項系結至資料來源時,每次進行系結時, Nodes 都會自動填入 和 ChildNodes 集合。 系結之間集合的任何變更都會遺失。 若要保留這些變更,請更新資料來源,或在每次系結時手動重建集合。

TreeNodeCollection包含屬性和方法,可讓您擷取集合本身的相關資訊。 若要瞭解集合中的專案數目,請使用 Count 屬性。 如果您想要判斷集合是否包含特定 TreeNode 物件,請使用 Contains 方法。 若要取得集合中物件的索引 TreeNode ,請使用 IndexOf 方法。

建構函式

TreeNodeCollection()

使用預設值,初始化 TreeNodeCollection 類別的新執行個體。

TreeNodeCollection(TreeNode)

使用指定的父節點 (或擁有人),初始化 TreeNodeCollection 類別的新執行個體。

屬性

Count

取得 TreeNodeCollection 物件中的項目數。

IsSynchronized

取得值,這個值表示對 TreeNodeCollection 的存取是否同步 (安全執行緒)。

Item[Int32]

取得 TreeNode 物件中位於指定索引處的 TreeNodeCollection 物件。

SyncRoot

取得可用來同步處理對 TreeNodeCollection 物件之存取的物件。

方法

Add(TreeNode)

將指定的 TreeNode 物件附加至 TreeNodeCollection 物件的結尾。

AddAt(Int32, TreeNode)

將指定的 TreeNode 物件插入指定索引位置的 TreeNodeCollection 物件。

Clear()

清空 TreeNodeCollection 物件。

Contains(TreeNode)

判斷指定的 TreeNode 物件是否在集合中。

CopyTo(TreeNode[], Int32)

從目標陣列中的指定索引開始,從 TreeNodeCollection 物件中,將所有項目複製至相容的一維陣列 TreeNode 物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回列舉值,可用來逐一查看 TreeNodeCollection 物件。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(TreeNode)

判斷指定之 TreeNode 物件的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(TreeNode)

TreeNode 物件移除指定的 TreeNodeCollection 物件。

RemoveAt(Int32)

TreeNode 物件的指定索引位置移除 TreeNodeCollection 物件。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

從目標陣列中的指定索引處開始,將 TreeNodeCollection 物件中的所有項目複製到相容的一維 Array 中。

IStateManager.IsTrackingViewState

取得值,指出 TreeNodeCollection 物件是否正在將變更儲存到它的檢視狀態。

IStateManager.LoadViewState(Object)

載入 TreeNodeCollection 物件先前儲存的檢視狀態。

IStateManager.SaveViewState()

將檢視狀態的變更儲存至 Object

IStateManager.TrackViewState()

指示 TreeNodeCollection 追蹤其檢視狀態的變更。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱