TreeNode.Depth Proprietà

Definizione

Ottiene la profondità del nodo.

public:
 property int Depth { int get(); };
[System.ComponentModel.Browsable(false)]
public int Depth { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Depth : int
Public ReadOnly Property Depth As Integer

Valore della proprietà

Profondità del nodo.

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come usare la Depth proprietà per determinare la profondità di un nodo. Inizializza tutti i nodi con una profondità di uno a uno stato selezionato. Per il corretto funzionamento di questo esempio, è necessario copiare i dati XML di esempio seguenti in un file denominato Newsgroup.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)
  {

    // Check the depth of a node as it is being bound to data.
    // Initialize the Checked property to true if the depth is 1.
    if(e.Node.Depth == 1)
    {

      e.Node.Checked = true;

    }
    else
    {

      e.Node.Checked = false;

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNode Checked Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNode Checked Example</h3>
    
      <asp:TreeView id="NewsgroupTreeView" 
        DataSourceID="NewsgroupXmlDataSource"
        OnTreeNodeDataBound="Data_Bound"
        ShowCheckBoxes="All"
        ExpandDepth="2"  
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="category" TextField="Name"/>
          <asp:TreeNodeBinding DataMember="group" TextField="Name"/>
        </DataBindings>
         
      </asp:TreeView>
      
      <asp:XmlDataSource id="NewsgroupXmlDataSource"  
        DataFile="Newsgroup.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)

    ' Check the depth of a node as it is being bound to data.
    ' Initialize the Checked property to true if the depth is 1.
    If e.Node.Depth = 1 Then

      e.Node.Checked = True

    Else

      e.Node.Checked = False

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNode Checked Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNode Checked Example</h3>
    
      <asp:TreeView id="NewsgroupTreeView" 
        DataSourceID="NewsgroupXmlDataSource"
        OnTreeNodeDataBound="Data_Bound"
        ShowCheckBoxes="All"
        ExpandDepth="2"  
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="category" TextField="Name"/>
          <asp:TreeNodeBinding DataMember="group" TextField="Name"/>
        </DataBindings>
         
      </asp:TreeView>
      
      <asp:XmlDataSource id="NewsgroupXmlDataSource"  
        DataFile="Newsgroup.xml"
        runat="server">
      </asp:XmlDataSource>
    
    </form>
  </body>
</html>

Il codice seguente è dati XML di esempio per l'esempio precedente.

<category name="news.microsoft.com">  
    <group name="microsoft.public.dotnet.framework.aspnet"/>  
    <group name="microsoft.public.dotnet.framework.aspnet.mobile"/>  
    <group name="microsoft.public.dotnet.framework.aspnet.webservices"/>  
</category>  

Commenti

Utilizzare la Depth proprietà per determinare la profondità del nodo. La profondità rappresenta il numero di livelli di gerarchia tra un nodo e il nodo radice. Ad esempio, un nodo radice ha una profondità pari a zero. Un elemento figlio del nodo radice ha una profondità di uno e così via.

Si applica a

Vedi anche