TreeNode.Expanded Propiedad

Definición

Obtiene o establece un valor que indica si el nodo está expandido.

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)

Valor de propiedad

true si el nodo está expandido, false si el nodo no está expandido, o null.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la Expanded propiedad para expandir mediante programación un nodo. Inicializa todos los nodos con una profundidad de uno a un estado expandido. Observe que cuando se expande el nodo raíz, sus nodos secundarios ya están expandidos. Para que este ejemplo funcione correctamente, debe copiar los datos XML de ejemplo siguientes en un archivo denominado 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>

El código siguiente es datos XML de ejemplo para el ejemplo anterior.

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

Comentarios

Utilice la Expanded propiedad para especificar o determinar si el nodo está expandido.

Puede expandir y contraer un nodo llamando a los Expand métodos y Collapse , respectivamente. También puede expandir y contraer un nodo y todos sus nodos secundarios llamando a los ExpandAll métodos y CollapseAll , respectivamente.

Dado que la Expanded propiedad es una propiedad de tres estados, el siguiente fragmento de código de C# produce un error de compilación:

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

Aunque VB.Net convierte implícitamente el Boolean valor en , NullableBooleanC# no. Por lo tanto, es un procedimiento recomendado comprobar explícitamente el estado de la propiedad. Por ejemplo, los siguientes ejemplos de código en Visual Basic y C# prueban explícitamente el valor de la Expanded propiedad .

En el siguiente ejemplo de código de Visual Basic se prueba explícitamente el valor de la Expanded propiedad . En este ejemplo se comprueba si la Expanded propiedad está establecida Trueen ; por lo tanto Nothing , y False pasa por la If instrucción .

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

En este ejemplo de código de C# se prueba explícitamente el valor de la Expanded propiedad . En este ejemplo se comprueba si la Expanded propiedad está establecida Trueen ; por lo tanto Null , y False pasa por la If instrucción .

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

Se aplica a

Consulte también