TreeNodeStyle 類別

定義

表示 TreeView 控制項中節點的樣式。

public ref class TreeNodeStyle sealed : System::Web::UI::WebControls::Style
public sealed class TreeNodeStyle : System.Web.UI.WebControls.Style
type TreeNodeStyle = class
    inherit Style
Public NotInheritable Class TreeNodeStyle
Inherits Style
繼承

範例

下列程式碼範例示範如何藉由設定從 ParentNodeStyle 屬性傳回之 物件的樣式屬性 TreeNodeStyle ,來控制控制項中 TreeView 父節點的外觀。


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

    // Programmatically set the HorizontalPadding property based on the 
    // user's selection.
    ItemsTreeView.ParentNodeStyle.HorizontalPadding = Convert.ToInt32(HorizontalPaddingList.SelectedItem.Text);

  }

  void VerticalPadding_Changed(Object sender, EventArgs e)
  {

    // Programmatically set the VerticalPadding property based on the 
    // user's selection.
    ItemsTreeView.ParentNodeStyle.VerticalPadding = Convert.ToInt32(VerticalPaddingList.SelectedItem.Text);

  }

  void NodeSpacing_Changed(Object sender, EventArgs e)
  {

    // Programmatically set the NodeSpacing property based on the 
    // user's selection.
    ItemsTreeView.ParentNodeStyle.NodeSpacing = Convert.ToInt32(NodeSpacingList.SelectedItem.Text);

  }

  void ChildNodePadding_Changed(Object sender, EventArgs e)
  {

    // Programmatically set the ChildNodesPadding property based on the 
    // user's selection.
    ItemsTreeView.ParentNodeStyle.ChildNodesPadding = Convert.ToInt32(ChildNodesPaddingList.SelectedItem.Text);

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeStyle Example</title>
</head>
<body>  
    <form id="form1" runat="server">
    
      <h3>TreeNodeStyle Example</h3>
      
      <!-- Set the styles for the leaf nodes declaratively. -->
      <asp:TreeView id="ItemsTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        ParentNodeStyle-ForeColor="Green"
        ParentNodeStyle-HorizontalPadding="5" 
        ParentNodeStyle-VerticalPadding="5"  
        ParentNodeStyle-NodeSpacing="5"
        ParentNodeStyle-ChildNodesPadding="5"
        ExpandDepth="4"  
        runat="server">
         
        <Nodes>
        
          <asp:TreeNode Text="Table of Contents"
            SelectAction="None">
             
            <asp:TreeNode Text="Chapter One">
            
              <asp:TreeNode Text="Section 1.0">
              
                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>
              
              </asp:TreeNode>
              
              <asp:TreeNode Text="Section 1.1">
              
                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
        
        </Nodes>
        
      </asp:TreeView>
      
      <hr />
      
      <h5>Select the style settings for the parent nodes.</h5>
      
      <table cellpadding="5">
      
        <tr align="right">
        
          <td>
          
            Horizontal Padding:
          
            <asp:DropDownList id="HorizontalPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="HorizontalPadding_Changed" 
              runat="server">
              
              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>
              
            </asp:DropDownList> 
          
          </td>
          
          <td>
          
            Vertical Padding:
          
            <asp:DropDownList id="VerticalPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="VerticalPadding_Changed" 
              runat="server">
              
              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>
              
            </asp:DropDownList> 
          
          </td>
          
        </tr>
        
        <tr align="right">
        
          <td>
          
            Node Spacing:
          
            <asp:DropDownList id="NodeSpacingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="NodeSpacing_Changed"   
              runat="server">
              
              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>
              
            </asp:DropDownList> 
          
          </td>
          
          <td>
          
            Child Nodes Padding:
          
            <asp:DropDownList id="ChildNodesPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="ChildNodePadding_Changed"  
              runat="server">
              
              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>
              
            </asp:DropDownList> 
          
          </td>
        
        </tr>
      
      </table>
       
    </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 HorizontalPadding_Changed(ByVal sender As Object, ByVal e As EventArgs)

    ' Programmatically set the HorizontalPadding property based on the 
    ' user's selection.
    ItemsTreeView.ParentNodeStyle.HorizontalPadding = Convert.ToInt32(HorizontalPaddingList.SelectedItem.Text)

  End Sub

  Sub VerticalPadding_Changed(ByVal sender As Object, ByVal e As EventArgs)

    ' Programmatically set the VerticalPadding property based on the 
    ' user's selection.
    ItemsTreeView.ParentNodeStyle.VerticalPadding = Convert.ToInt32(VerticalPaddingList.SelectedItem.Text)

  End Sub

  Sub NodeSpacing_Changed(ByVal sender As Object, ByVal e As EventArgs)

    ' Programmatically set the NodeSpacing property based on the 
    ' user's selection.
    ItemsTreeView.ParentNodeStyle.NodeSpacing = Convert.ToInt32(NodeSpacingList.SelectedItem.Text)

  End Sub

  Sub ChildNodePadding_Changed(ByVal sender As Object, ByVal e As EventArgs)

    ' Programmatically set the ChildNodesPadding property based on the 
    ' user's selection.
    ItemsTreeView.ParentNodeStyle.ChildNodesPadding = Convert.ToInt32(ChildNodesPaddingList.SelectedItem.Text)

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeStyle Example</title>
</head>
<body>  
    <form id="form1" runat="server">
    
      <h3>TreeNodeStyle Example</h3>
      
      <!-- Set the styles for the leaf nodes declaratively. -->
      <asp:TreeView id="ItemsTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        ParentNodeStyle-ForeColor="Green"
        ParentNodeStyle-HorizontalPadding="5" 
        ParentNodeStyle-VerticalPadding="5"  
        ParentNodeStyle-NodeSpacing="5"
        ParentNodeStyle-ChildNodesPadding="5"
        ExpandDepth="4"  
        runat="server">
         
        <Nodes>
        
          <asp:TreeNode Text="Table of Contents"
            SelectAction="None">
             
            <asp:TreeNode Text="Chapter One">
            
              <asp:TreeNode Text="Section 1.0">
              
                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>
              
              </asp:TreeNode>
              
              <asp:TreeNode Text="Section 1.1">
              
                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
        
        </Nodes>
        
      </asp:TreeView>
      
      <hr />
      
      <h5>Select the style settings for the parent nodes.</h5>
      
      <table cellpadding="5">
      
        <tr align="right">
        
          <td>
          
            Horizontal Padding:
          
            <asp:DropDownList id="HorizontalPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="HorizontalPadding_Changed" 
              runat="server">
              
              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>
              
            </asp:DropDownList> 
          
          </td>
          
          <td>
          
            Vertical Padding:
          
            <asp:DropDownList id="VerticalPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="VerticalPadding_Changed" 
              runat="server">
              
              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>
              
            </asp:DropDownList> 
          
          </td>
          
        </tr>
        
        <tr align="right">
        
          <td>
          
            Node Spacing:
          
            <asp:DropDownList id="NodeSpacingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="NodeSpacing_Changed"   
              runat="server">
              
              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>
              
            </asp:DropDownList> 
          
          </td>
          
          <td>
          
            Child Nodes Padding:
          
            <asp:DropDownList id="ChildNodesPaddingList"
              AutoPostBack="true"
              OnSelectedIndexChanged="ChildNodePadding_Changed"  
              runat="server">
              
              <asp:ListItem>0</asp:ListItem>
              <asp:ListItem Selected="true">5</asp:ListItem>
              <asp:ListItem>10</asp:ListItem>
              
            </asp:DropDownList> 
          
          </td>
        
        </tr>
      
      </table>
       
    </form>
  </body>
</html>

備註

使用 TreeNodeStyle 類別來表示 控制項中節點的 TreeView 樣式。 控制項 TreeView 可讓您指定不同的樣式特性 (,例如每個不同節點類型的字型大小和色彩) 。

下表列出 類別支援的 TreeNodeStyle 節點樣式。

節點樣式屬性 描述
HoverNodeStyle 當滑鼠指標位於節點上方時,節點的樣式設定。
LeafNodeStyle 分葉節點的樣式設定。
NodeStyle 節點的預設樣式設定。
ParentNodeStyle 父節點的樣式設定。
RootNodeStyle 根節點的樣式設定。
SelectedNodeStyle 所選節點的樣式設定。

當您設定控制項的 TreeView 節點樣式屬性時,會依照下列順序套用這些屬性:

  1. NodeStyle.

  2. RootNodeStyleParentNodeStyle 、 或 LeafNodeStyle ,視節點類型而定。

  3. LevelStyles.

  4. SelectedNodeStyle.

  5. HoverNodeStyle.

類別 TreeNodeStyle 會從 Style 類別繼承大部分的成員。 它提供屬性來控制節點中文字周圍的空間量,以及相鄰節點之間的空間,藉此擴充 Style 類別。 HorizontalPadding使用 屬性來控制節點中文字左邊和右邊的空間量。 同樣地, VerticalPadding 屬性會控制節點中文字上方和下方的空間量。 您可以藉由設定 NodeSpacing 屬性來控制套用 的節點 TreeNodeStyle 與其相鄰節點之間的間距量。 若要控制父節點與子節點之間的間距量,請使用 ChildNodesPadding 屬性。

如需繼承樣式設定的詳細資訊,請參閱 Style

建構函式

TreeNodeStyle()

初始化 TreeNodeStyle 類別的新執行個體。

TreeNodeStyle(StateBag)

使用指定的 TreeNodeStyle 物件資訊,初始化 StateBag 類別的新執行個體。

屬性

BackColor

取得或設定 Web 伺服器控制項的背景色彩。

(繼承來源 Style)
BorderColor

取得或設定 Web 伺服器控制項的框線色彩。

(繼承來源 Style)
BorderStyle

取得或設定 Web 伺服器控制項的框線樣式。

(繼承來源 Style)
BorderWidth

取得或設定 Web 伺服器控制項的框線寬度。

(繼承來源 Style)
CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
ChildNodesPadding

取得或設定父節點與套用 TreeNodeStyle 類別之子節點之間的間距。

Container

取得包含 IContainerComponent

(繼承來源 Component)
CssClass

取得或設定用戶端上 Web 伺服器控制項所呈現的階層式樣式表 (CSS)。

(繼承來源 Style)
DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
Font

取得與 Web 伺服器控制項關聯的字型屬性。

(繼承來源 Style)
ForeColor

取得或設定 Web 伺服器控制項的前景色彩 (通常是文字的色彩)。

(繼承來源 Style)
Height

取得或設定 Web 伺服器控制項的高度。

(繼承來源 Style)
HorizontalPadding

取得或設定節點中文字左側和右側的間距。

ImageUrl

取得或設定節點旁顯示之影像的 URL。

IsEmpty

保護的 (Protected) 屬性。 取得值,指出狀態包中是否已定義任何樣式項目。

(繼承來源 Style)
IsTrackingViewState

傳回值,指出是否任何的樣式項目都已經在狀態包中定義。

(繼承來源 Style)
NodeSpacing

取得或設定套用 TreeNodeStyle 物件之節點與其相鄰節點之間的垂直間距。

RegisteredCssClass

取得使用控制項註冊的階層式樣式表 (CSS) 類別。

(繼承來源 Style)
Site

取得或設定 ComponentISite

(繼承來源 Component)
VerticalPadding

取得或設定節點文字上方和下方的間距。

ViewState

取得保存樣式項目的狀態包。

(繼承來源 Style)
Width

取得或設定 Web 伺服器控制項的寬度。

(繼承來源 Style)

方法

AddAttributesToRender(HtmlTextWriter)

將需要呈現的 HTML 屬性和樣式加入至指定的 HtmlTextWriter 中。 這個方法主要由控制項開發人員使用。

(繼承來源 Style)
AddAttributesToRender(HtmlTextWriter, WebControl)

將需要呈現的 HTML 屬性和樣式加入至指定的 HtmlTextWriter 和 Web 伺服器控制項中。 這個方法主要由控制項開發人員使用。

(繼承來源 Style)
CopyFrom(Style)

將指定之 Style 物件的樣式屬性複製至目前的 TreeNodeStyle 物件。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FillStyleAttributes(CssStyleCollection, IUrlResolutionService)

將指定物件的樣式屬性加入 CssStyleCollection 物件中。

(繼承來源 Style)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已過時。

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetStyleAttributes(IUrlResolutionService)

擷取指定 CssStyleCollection 實作物件的 IUrlResolutionService 物件。

(繼承來源 Style)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已過時。

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
LoadViewState(Object)

載入先前儲存的狀態。

(繼承來源 Style)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
MergeWith(Style)

將指定之 Style 物件的樣式屬性與目前 TreeNodeStyle 物件的樣式屬性組合。

Reset()

TreeNodeStyle 物件返回至其原始狀態。

SaveViewState()

保護的 (Protected) 方法。 儲存叫用 TrackViewState() 方法之後,任何已變更的狀態。

(繼承來源 Style)
SetBit(Int32)

保護的內部方法。 設定內部位元遮罩欄位,表示儲存在狀態包中的樣式屬性。

(繼承來源 Style)
SetDirty()

標記 Style,以便將其狀態記錄在檢視狀態中。

(繼承來源 Style)
ToString()

傳回代表目前物件的字串。

(繼承來源 Style)
TrackViewState()

保護的 (Protected) 方法。 在控制項上標記追蹤狀態變更的開頭。 任何在追蹤開始後的變更都會被追蹤,並儲存成控制項檢視狀態的一部分。

(繼承來源 Style)

事件

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)

明確介面實作

IStateManager.IsTrackingViewState

取得值,指出伺服器控制項是否正在追蹤其檢視狀態變更。

(繼承來源 Style)
IStateManager.LoadViewState(Object)

載入先前儲存的狀態。

(繼承來源 Style)
IStateManager.SaveViewState()

傳回包含狀態變更的物件。

(繼承來源 Style)
IStateManager.TrackViewState()

啟動追蹤狀態的變更。

(繼承來源 Style)

適用於

另請參閱