TreeNode.Depth Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém a profundidade do nó.
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
Valor de Propriedade
A profundidade do nó.
- Atributos
Exemplos
O exemplo de código seguinte demonstra como usar a Depth propriedade para determinar a profundidade de um nó. Inicializa todos os nós com profundidade de um para um estado selecionado. Para que este exemplo funcione corretamente, deve copiar os dados XML de exemplo abaixo para um ficheiro chamado 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>
O código seguinte é um exemplo de dados XML para o exemplo anterior.
<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>
Observações
Use a Depth propriedade para determinar a profundidade do nó. A profundidade representa o número de níveis de hierarquia entre um nó e o nó raiz. Por exemplo, um nó raiz tem uma profundidade zero. Um filho do nó raiz tem profundidade de um, e assim sucessivamente.