TreeNode.Expanded 屬性

定義

取得或設定值,表示節點是否已展開。

public:
 property Nullable<bool> Expanded { Nullable<bool> get(); void set(Nullable<bool> value); };
public bool? Expanded { get; set; }
member this.Expanded : Nullable<bool> with get, set
Public Property Expanded As Nullable(Of Boolean)

屬性值

如果節點已展開則為 true;如果節點未展開則為 false;或是 null

範例

下列程式碼範例示範如何使用 Expanded 屬性,以程式設計方式展開節點。 它會初始化具有一個深度到展開狀態的所有節點。 請注意,當根節點展開時,其子節點已經展開。 若要讓此範例正常運作,您必須將下面的範例 XML 資料複製到名為 Book.xml 的檔案。


<%@ 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 Data_Bound(Object sender, TreeNodeEventArgs e)
  {

    // Determine the depth of a node as it is bound to data.
    // If the depth is 1, expand the node.
    if(e.Node.Depth == 1)
    {

      e.Node.Expanded = true;

    }
    else
    {

      e.Node.Expanded = false;

    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNode Expanded Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNode Expanded Example</h3>
    
      <asp:TreeView id="BookTreeView" 
        DataSourceID="BookXmlDataSource"
        OnTreeNodeDataBound="Data_Bound"
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
          <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
          <asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>
        </DataBindings>
         
      </asp:TreeView>
      
      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>
    
    </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 Data_Bound(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
        ' Determine the depth of a node as it is bound to data.
        ' If the depth is 1, expand the node.
        If e.Node.Depth = 1 Then
            e.Node.Expanded = True
        Else
            e.Node.Expanded = False
        End If
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNode Expanded Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNode Expanded Example</h3>
    
      <asp:TreeView id="BookTreeView" 
        DataSourceID="BookXmlDataSource"
        OnTreeNodeDataBound="Data_Bound"
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
          <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
          <asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>
        </DataBindings>
         
      </asp:TreeView>
      
      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>
    
    </form>
  </body>
</html>

下列程式碼是上一個範例的 XML 資料範例。

<Book Title="Book Title">  
    <Chapter Heading="Chapter 1">  
        <Section Heading="Section 1">  
        </Section>  
        <Section Heading="Section 2">  
        </Section>  
    </Chapter>  
    <Chapter Heading="Chapter 2">  
        <Section Heading="Section 1">  
        </Section>  
    </Chapter>  
</Book>  

備註

Expanded使用 屬性來指定或判斷節點是否已展開。

您可以分別呼叫 ExpandCollapse 方法來展開和折迭節點。 您也可以分別呼叫 ExpandAllCollapseAll 方法來展開和折迭節點及其所有子節點。

Expanded由於 屬性是三狀態屬性,因此下列 C# 程式碼片段會造成編譯錯誤:

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)  
{  
if (TreeView1.Nodes[0].Expanded)  
{  
// some work here   
}  
}  

雖然 VB.Net 隱含地將 BooleanNullableBoolean 轉換成 ,但 C# 則不會。 因此,最好明確地檢查屬性的狀態。 例如,Visual Basic 和 C# 中的下列程式碼範例會明確測試 屬性的值 Expanded

下列 Visual Basic 程式碼範例會明確測試 屬性的值 Expanded 。 這個範例會測試 屬性是否設定為 True ,因此 NothingFalse 落在 語句中 IfExpanded

If TreeView1.Nodes(0).Expanded = True Then 'some work hereEnd IF  

這個 C# 程式碼範例會明確測試 屬性的值 Expanded 。 這個範例會測試 屬性是否設定為 True ,因此 NullFalse 落在 語句中 IfExpanded

if( TreeView1.Nodes[0].Expanded == true ) { //some work here}  

適用於

另請參閱