WPF and Silverlight Edition Basic Library / Menu and ContextMenu / Menu and ContextMenu Quick Start / Step 4 of 5: Adding a C1ContextMenu to the C1Menu Control
Step 4 of 5: Adding a C1ContextMenu to the C1Menu Control

In the last step, you added submenus to two of the C1Menu control's menu items. In this step, you will add a C1ContextMenu control to the C1Menu control. This context menu will have one item that, when clicked, will add submenu items to the C1Menu control's top-level "Added Items" top-level menu item that you created in Step 2 of 5: Adding Menu Items

Complete the following steps:

  1. In XAML view, place the following XAML markup right before the </c1:C1Menu> tag:
XAML
Copy Code
<c1:C1ContextMenuService.ContextMenu>
     <c1:C1ContextMenu Width="Auto" Height="Auto">
          <c1:C1MenuItem Height="Auto" Width="Auto" Header="Add Item" Click="C1MenuItem_AddOnClick"/>               
     </c1:C1ContextMenu>
</c1:C1ContextMenuService.ContextMenu>
The above markup adds a C1ContextMenu control to the C1Menu control using the C1ContextMenuService helper class. Note that the C1ContextMenu control contains one C1MenuItem that is attached to a Click event named "C1MenuItem_AddOnClick".
  1. Add x:Name="AddedItems" to the <c1:C1MenuItem Header="Added Items"/> tag. This gives the item a unique identifier so that you can call it in code. The markup will resemble the following:
XAML
Copy Code
<c1:C1MenuItem Height="Auto" Width="Auto" Header="Added Items"  x:Name="AddedItems"></c1:C1MenuItem>
  1. Open the MainPage.xaml.cs page and add the following Click event handler to the project:
Visual Basic
Copy Code
Private Sub C1MenuItem_AddOnClick(ByVal sender As Object, ByVal
e As C1.Silverlight.SourcedEventArgs)
      Dim C1MenuItemAdd As New C1.Silverlight.C1MenuItem()
      C1MenuItemAdd.Header = "Added Item"
      AddedItems.Items.Add(C1MenuItemAdd)
 End Sub

 

C#
Copy Code
private void C1MenuItem_AddOnClick(object sender, C1.Silverlight.SourcedEventArgs e)
    {
         C1.Silverlight.C1MenuItem C1MenuItemAdd = new C1.Silverlight.C1MenuItem();
         C1MenuItemAdd.Header = "Added Item";
         AddedItems.Items.Add(C1MenuItemAdd);
    }

In this step, you added a C1ContextMenu control to the C1Menu control. In the next step, you will run the project and see the result of the Menu for Silverlight quick start.