TreeNode.Expanded Proprietà

Definizione

Ottiene o imposta un valore che indica se il nodo è espanso.

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)

Valore della proprietà

true se il nodo è espanso, false se il nodo non è espanso o null.

Esempio

Nell'esempio di codice seguente viene illustrato come usare la Expanded proprietà per espandere a livello di codice un nodo. Inizializza tutti i nodi con una profondità di uno a uno stato espanso. Si noti che quando il nodo radice viene espanso, i nodi figlio sono già espansi. Per il corretto funzionamento di questo esempio, è necessario copiare i dati XML di esempio seguenti in un file denominato 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>

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

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

Commenti

Utilizzare la Expanded proprietà per specificare o determinare se il nodo è espanso.

È possibile espandere e comprimere un nodo chiamando rispettivamente i Expand metodi e Collapse . È anche possibile espandere e comprimere un nodo e tutti i relativi nodi figlio chiamando rispettivamente i ExpandAll metodi e CollapseAll .

Poiché la Expanded proprietà è una proprietà tri-state, il frammento di codice C# seguente genera un errore di compilazione:

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

Anche se VB.Net esegue il cast implicito del Boolean valore a NullableBoolean, C# non esegue il cast implicito. Di conseguenza, è consigliabile controllare in modo esplicito lo stato della proprietà. Ad esempio, gli esempi di codice seguenti in Visual Basic e C# testano in modo esplicito il valore della Expanded proprietà .

Nell'esempio di codice visual Basic seguente viene verificato in modo esplicito il valore della Expanded proprietà . In questo esempio viene verificato se la Expanded proprietà è impostata su Truee False quindi Nothing viene eseguita l'istruzione If .

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

Questo esempio di codice C# verifica in modo esplicito il valore della Expanded proprietà. In questo esempio viene verificato se la Expanded proprietà è impostata su Truee False quindi Null viene eseguita l'istruzione If .

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

Si applica a

Vedi anche