MenuItemCollection クラス

定義

Menu コントロールに含まれるメニュー項目のコレクションを表します。 このクラスは継承できません。

public ref class MenuItemCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class MenuItemCollection : System.Collections.ICollection, System.Web.UI.IStateManager
type MenuItemCollection = class
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public NotInheritable Class MenuItemCollection
Implements ICollection, IStateManager
継承
MenuItemCollection
実装

次のコード例では、宣言構文を使用して コレクションと ChildItems コレクションを設定Itemsする方法を示します。


<%@ 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" >

  <!-- For the hover styles of the Menu control to  -->
  <!-- work correctly, you must include this head   -->
  <!-- element.                                     -->
  <head runat="server">
    <title>Menu Declarative Example</title>
</head>

  <body>
    <form id="form1" runat="server">
    
      <h3>Menu Declarative Example</h3>
    
      <!-- Use declarative syntax to create the   -->
      <!-- menu structure. Submenu items are      -->
      <!-- created by nesting them in parent menu -->
      <!-- items.                                 -->
      <asp:menu id="NavigationMenu"
        disappearafter="2000"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        font-names="Arial" 
        target="_blank"  
        runat="server">
        
        <staticmenuitemstyle backcolor="LightSteelBlue"
          forecolor="Black"/>
        <statichoverstyle backcolor="LightSkyBlue"/>
        <dynamicmenuitemstyle backcolor="Black"
          forecolor="Silver"/>
        <dynamichoverstyle backcolor="LightSkyBlue"
          forecolor="Black"/>
      
        <items>
          <asp:menuitem navigateurl="Home.aspx" 
            text="Home"
            tooltip="Home">
            <asp:menuitem navigateurl="Music.aspx"
              text="Music"
              tooltip="Music">
              <asp:menuitem navigateurl="Classical.aspx" 
                text="Classical"
                tooltip="Classical"/>
              <asp:menuitem navigateurl="Rock.aspx"
                text="Rock"
                tooltip="Rock"/>
              <asp:menuitem navigateurl="Jazz.aspx"
                text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem navigateurl="Movies.aspx"
              text="Movies"
              tooltip="Movies">
              <asp:menuitem navigateurl="Action.aspx"
                text="Action"
                tooltip="Action"/>
              <asp:menuitem navigateurl="Drama.aspx"
                text="Drama"
                tooltip="Drama"/>
              <asp:menuitem navigateurl="Musical.aspx"
                text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>
      
      </asp:menu>

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

  <!-- For the hover styles of the Menu control to  -->
  <!-- work correctly, you must include this head   -->
  <!-- element.                                     -->
  <head runat="server">
    <title>Menu Declarative Example</title>
</head>

  <body>
    <form id="form1" runat="server">
    
      <h3>Menu Declarative Example</h3>
    
      <!-- Use declarative syntax to create the   -->
      <!-- menu structure. Submenu items are      -->
      <!-- created by nesting them in parent menu -->
      <!-- items.                                 -->
      <asp:menu id="NavigationMenu"
        disappearafter="2000"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        font-names="Arial" 
        target="_blank"  
        runat="server">
        
        <staticmenuitemstyle backcolor="LightSteelBlue"
          forecolor="Black"/>
        <statichoverstyle backcolor="LightSkyBlue"/>
        <dynamicmenuitemstyle backcolor="Black"
          forecolor="Silver"/>
        <dynamichoverstyle backcolor="LightSkyBlue"
          forecolor="Black"/>
      
        <items>
          <asp:menuitem navigateurl="Home.aspx" 
            text="Home"
            tooltip="Home">
            <asp:menuitem navigateurl="Music.aspx"
              text="Music"
              tooltip="Music">
              <asp:menuitem navigateurl="Classical.aspx" 
                text="Classical"
                tooltip="Classical"/>
              <asp:menuitem navigateurl="Rock.aspx"
                text="Rock"
                tooltip="Rock"/>
              <asp:menuitem navigateurl="Jazz.aspx"
                text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem navigateurl="Movies.aspx"
              text="Movies"
              tooltip="Movies">
              <asp:menuitem navigateurl="Action.aspx"
                text="Action"
                tooltip="Action"/>
              <asp:menuitem navigateurl="Drama.aspx"
                text="Drama"
                tooltip="Drama"/>
              <asp:menuitem navigateurl="Musical.aspx"
                text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>
      
      </asp:menu>

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

次のコード例では、ルート メニュー項目のコレクションにオブジェクトをMenuItemChildItemsプログラムで追加する方法を示します。


<%@ 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 Page_Load(Object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      // Retrieve the root menu item from the Items
      // collection of the Menu control using the indexer.
      MenuItem homeMenuItem = NavigationMenu.Items[0];

      // Create the submenu item.
      MenuItem newSubMenuItem = new MenuItem("New Category");

      // Add the submenu item to the ChildItems
      // collection of the root menu item.
      homeMenuItem.ChildItems.Add(newSubMenuItem);
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection Add Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection Add Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server">
        
        <items>
          <asp:menuitem text="Home"
            tooltip="Home">
            <asp:menuitem text="Music"
              tooltip="Music">
              <asp:menuitem text="Classical"
                tooltip="Classical"/>
              <asp:menuitem text="Rock"
                tooltip="Rock"/>
              <asp:menuitem text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem text="Movies"
              tooltip="Movies">
              <asp:menuitem text="Action"
                tooltip="Action"/>
              <asp:menuitem text="Drama"
                tooltip="Drama"/>
              <asp:menuitem text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>

      </asp:menu>

    </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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    If Not IsPostBack Then

      ' Retrieve the root menu item from the Items
      ' collection of the Menu control using the indexer.
      Dim homeMenuItem As MenuItem = NavigationMenu.Items(0)

      ' Create the submenu item.
      Dim newSubMenuItem = New MenuItem("New Category")

      ' Add the submenu item to the ChildItems
      ' collection of the root menu item.
      homeMenuItem.ChildItems.Add(newSubMenuItem)
    
    End If
      
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection Add Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection Add Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server">
        
        <items>
          <asp:menuitem text="Home"
            tooltip="Home">
            <asp:menuitem text="Music"
              tooltip="Music">
              <asp:menuitem text="Classical"
                tooltip="Classical"/>
              <asp:menuitem text="Rock"
                tooltip="Rock"/>
              <asp:menuitem text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem text="Movies"
              tooltip="Movies">
              <asp:menuitem text="Action"
                tooltip="Action"/>
              <asp:menuitem text="Drama"
                tooltip="Drama"/>
              <asp:menuitem text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>

      </asp:menu>

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

注釈

クラスはMenuItemCollection、コントロール内MenuのオブジェクトのMenuItemコレクションを格納および管理するために使用されます。 コントロールは Menu 、 クラスを MenuItemCollection 使用して、そのルート メニュー項目を プロパティに Items 格納します。 このコレクションは、メニュー項目のMenuItemサブメニュー項目 (存在する場合) を格納するオブジェクトの プロパティにも使用ChildItemsされます。

クラスでは MenuItemCollection 、コレクション内の項目にアクセスするいくつかの方法がサポートされています。

  • インデクサーを Item[] 使用して、特定の MenuItem 0 から始まるインデックス位置にあるオブジェクトを直接取得します。

  • メソッドを GetEnumerator 使用して、コレクションを反復処理するために使用できる列挙子を作成します。

  • コレクションの CopyTo 内容を配列にコピーするには、 メソッドを使用します。

オブジェクトを追加および削除MenuItemすることで、MenuItemCollectionプログラムでオブジェクトを管理できます。 メニュー項目をコレクションに追加するには、 メソッドまたは メソッドをAddAt使用Addします。 コレクションからノードを削除するには、 メソッド、 RemoveRemoveAtまたは メソッドを Clear 使用します。

注意

コントロールが Menu データ ソースにバインドされると、 Items コレクションと ChildItems コレクションは、バインドが行われるたびに自動的に設定されます。 バインド間のコレクションに対する変更はすべて失われます。 これらの変更を保持するには、データ ソースを更新するか、バインドするたびにコレクションを手動で再構築します。

クラスには MenuItemCollection 、コレクション自体に関する情報を取得できるプロパティとメソッドが含まれています。 コレクション内の項目の数を確認するには、 プロパティを Count 使用します。 コレクションに特定 MenuItem のオブジェクトが含まれているかどうかを判断する場合は、 メソッドを Contains 使用します。 コレクション内のオブジェクトのインデックスを MenuItem 取得するには、 メソッドを IndexOf 使用します。

コンストラクター

MenuItemCollection()

MenuItemCollection クラスの新しいインスタンスを既定値で初期化します。

MenuItemCollection(MenuItem)

親メニュー項目 (または所有者) を指定して、MenuItemCollection クラスの新しいインスタンスを初期化します。

プロパティ

Count

現在の MenuItemCollection オブジェクト内のメニュー項目の数を取得します。

IsSynchronized

MenuItemCollection オブジェクトへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

Item[Int32]

現在の MenuItem オブジェクト内の指定したインデックス位置にある MenuItemCollection オブジェクトを取得します。

SyncRoot

MenuItemCollection オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。

メソッド

Add(MenuItem)

指定した MenuItem オブジェクトを現在の MenuItemCollection オブジェクトの末尾に追加します。

AddAt(Int32, MenuItem)

指定した MenuItem オブジェクトを、現在の MenuItemCollection オブジェクトの指定したインデックス位置に挿入します。

Clear()

現在の MenuItemCollection オブジェクトからすべての項目を削除します。

Contains(MenuItem)

指定した MenuItem オブジェクトがコレクション内にあるかどうかを確認します。

CopyTo(Array, Int32)

MenuItemCollection オブジェクトのすべての項目を、互換性のある 1 次元の Array にコピーします。コピー先の配列の指定したインデックスからコピーが開始されます。

CopyTo(MenuItem[], Int32)

MenuItemCollection のすべての項目を互換性のある 1 次元の MenuItem オブジェクト配列にコピーします。コピー先の配列の指定したインデックスからコピーが開始されます。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

現在の MenuItemCollection オブジェクト内の項目を反復処理するために使用できる列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IndexOf(MenuItem)

コレクション内の指定した MenuItem オブジェクトのインデックスを確認します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(MenuItem)

指定した MenuItem オブジェクトを MenuItemCollection オブジェクトから削除します。

RemoveAt(Int32)

指定したインデックス位置にある MenuItem オブジェクトを現在の MenuItemCollection オブジェクトから削除します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

IStateManager.IsTrackingViewState

MenuItemCollection オブジェクトがビューステートへの変更を保存しているかどうかを示す値を取得します。

IStateManager.LoadViewState(Object)

MenuItemCollection オブジェクトが前回保存したビューステートを読み込みます。

IStateManager.SaveViewState()

ビューステートへの変更を Object に保存します。

IStateManager.TrackViewState()

ビュー ステートへの変更を追跡するように MenuItemCollection オブジェクトに指示します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

こちらもご覧ください