語言

MenuItemBinding.Depth 屬性

定義

取得或設定物件套用的選單深度 MenuItemBinding 。

public:
 property int Depth { int get(); void set(int value); };
[System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.TreeNodeBindingDepthConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public int Depth { get; set; }
[System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.TreeNodeBindingDepthConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public int Depth { get; set; }
[<System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.TreeNodeBindingDepthConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.Depth : int with get, set
[<System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.TreeNodeBindingDepthConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.Depth : int with get, set
Public Property Depth As Integer

屬性值

是應用於選單深度 MenuItemBinding 的範圍。 預設值為 -1,表示此屬性未被設定。

屬性

範例

以下程式碼範例示範如何使用該 Depth 屬性指定選單深度,以套用 MenuItemBinding 物件。 為了讓這個範例正確運作,你必須將下方的範例 XML 資料複製到一個名為 MenuDepth.xml的檔案中。


<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemBinding Depth Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>MenuItemBinding Depth Example</h3>

      <asp:menu id="NavigationMenu"
        datasourceid="MenuSource"
        runat="server">
        
        <databindings>
        
          <asp:menuitembinding depth="0"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_self" />
          <asp:menuitembinding depth="1"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          <asp:menuitembinding depth="2"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          
        </databindings>
        
      </asp:menu>

      <asp:xmldatasource id="MenuSource"
        datafile="MenuDepth.xml"
        runat="server"/> 

    </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemBinding Depth Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>MenuItemBinding Depth Example</h3>

      <asp:menu id="NavigationMenu"
        datasourceid="MenuSource"
        runat="server">
        
        <databindings>
        
          <asp:menuitembinding depth="0"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_self" />
          <asp:menuitembinding depth="1"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          <asp:menuitembinding depth="2"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          
        </databindings>
        
      </asp:menu>

      <asp:xmldatasource id="MenuSource"
        datafile="MenuDepth.xml"
        runat="server"/> 

    </form>
  </body>
</html>

以下程式碼為前述範例的現場地圖範例資料。

<MapNode ImageUrl="~\Images\Home.gif"

Title="Home"

Description="Root Page"

ToolTip="Home Page">

<MapNode ImageUrl="~\Images\Music.gif"

Title="Music"

Description="Music Category"

ToolTip="Music Page">

<MapNode ImageUrl="~\Images\Classical.gif"

Title="Classical"

Description="Classical Section"

ToolTip="Classical Page"/>

<MapNode ImageUrl="~\Images\Rock.gif"

Title="Rock"

Description="Rock Section"

ToolTip="Rock Page"/>

<MapNode ImageUrl="~\Images\Jazz.gif"

Title="Jazz"

Description="Jazz Section"

ToolTip="Jazz Page"/>

</MapNode>

<MapNode ImageUrl="~\Images\Movies.gif"

Title="Movies"

Description="Movies Category"

ToolTip="Movies Page">

<MapNode ImageUrl="~\Images\Action.gif"

Title="Action"

Description="Action Section"

ToolTip="Action Page"/>

<MapNode ImageUrl="~\Images\Drama.gif"

Title="Drama"

Description="Drama Section"

ToolTip="Drama Page"/>

<MapNode ImageUrl="~\Images\Musical.gif"

Title="Musical"

Description="Musical Section"

ToolTip="Musical Page"/>

</MapNode>

</MapNode>

備註

建立 MenuItemBinding 物件時,必須指定綁定的條件。 這些標準會指出何時資料項目應該綁定到選單項目。 你可以指定 Depth 屬性、屬性, DataMember 或兩者都指定。

使用該 Depth 屬性指定選 MenuItemBinding 單深度,該物件應用於此。 例如,以下 MenuItemBinding 宣告將資料來源的名稱欄位與 ID 欄位綁定到 Text 所有深度為 0 的選單項目的 and Value 屬性:

<asp:MenuItemBinding Depth="0" TextField="Name" ValueField="ID">

有時你可能需要建立一個選單項目綁定,同時指定深度和資料成員。 當資料來源包含不同層級且具有相同資料成員值的項目時,通常會使用此方法。 例如,你可以讓 <Item> 元素出現在 XML 檔案的不同層級。 以下 MenuItemBinding 宣告說明如何指定適用於不同選單深度相同資料成員的選單項目綁定:

<asp:MenuItemBinding DataMember="Item" Depth="1" TextField="Title">

<asp:MenuItemBinding DataMember="Item" Depth="2" TextField="ISBN">

若選單項目綁定未定義深度與資料成員,則該選單項目綁定會套用至選單內所有選單項目。 當所有資料項目具有相同屬性且應以相同方式顯示時,無論選單深度如何,這通常會被使用。

在建立綁定條件後,你就可以將物件的 MenuItem 屬性綁定到一個值上。 你可以綁定到資料項目的欄位或靜態值。 綁定到靜態值時,所有 MenuItem 套用該 MenuItemBinding 物件的物件值相同。 綁定到欄位的屬性包含該欄位來自資料來源的值。

若定義了衝突 MenuItemBinding 物件, Menu 控制項會依以下優先順序套用選單項目綁定:

  1. 定義 MenuItemBinding 並匹配深度與資料成員的物件。

  2. 僅 MenuItemBinding 定義並匹配資料成員的物件。

  3. 只定義 MenuItemBinding 並匹配深度的物件。

  4. 這個 MenuItemBinding 物件既不定義深度,也不定義資料成員。 (這種選單項目綁定會套用到選單中的所有選單項目。)

  5. 那個 MenuItemBinding 在資料來源中沒有匹配的物件。 在這種情況下,資料項目的方法回傳ToString()的值會綁定到Text該物件所應用的選單項目Value的屬性MenuItemBinding。

適用於

另請參閱