Infragistics(R) NetAdvantage(R) : WPF 2012.1
Adding xamContextMenu to Your Page
E-mail your feedback on this topic.
Shared XAML > Menus > xamContextMenu > Adding xamContextMenu to Your Page

Glossary Item Box

Before You Begin

You can use the xamContextMenu™ control to create a context-sensitive menu for the different controls in your application. For example, you can create a context menu for a TextBox control that features undo or redo functionality and a context menu for a xamGrid control that features add or delete functionality.

What You Will Accomplish

You will learn how to add a xamContextMenu control to a text box. When you run the finished project and right-click the text box, you should see a context menu in your browser that looks similar to the screen shot below.

Follow these Steps

  1. Create a Microsoft® WPF™ application.
  2. In the Solution Explorer, add the following references to your project:
    • InfragisticsWPF4.v12.1.dll
    • InfragisticsWPF4.Controls.Menus.XamMenu.v12.1.dll
  3. Place using/Imports directives in your code-behind or add an XML namespace declaration for xamContextMenu.

    In XAML:

    xmlns:ig="http://schemas.infragistics.com/xaml"
    

    In Visual Basic:

    Imports Infragistics.Controls.Menus
    

    In C#:

    using Infragistics.Controls.Menus;
    
  4. Add a TextBox control to the default Grid layout panel named LayoutRoot. If you are doing this in procedural code, you can handle the user control's Loaded event and place the code in the event handler.

    In XAML:

    <TextBox Name="textBox1">
        <!--TODO: Declare tags for the ContextMenuService class' attached Manager property-->
    </TextBox>
    

    In Visual Basic:

    Dim textbox1 As TextBox
    
    Private Sub UserControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        textBox1 = New TextBox()
        Me.LayoutRoot.Children.Add(textBox1)
        
        'TODO: Instantiate a ContextMenuManager object and attach it to the TextBox control
        'TODO: Instantiate a xamContextMenu control
        'TODO: Add MenuItem objects to the xamContextMenu control
    End Sub
    

    In C#:

    private TextBox textBox1;
    
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        textBox1 = new TextBox();
        this.LayoutRoot.Children.Add(textBox1);
    
        //TODO: Instantiate a ContextMenuManager object and attach it to the TextBox control
        //TODO: Instantiate a xamContextMenu control
        //TODO: Add MenuItem objects to the xamContextMenu control
    }
    
  5. Declare tags for the ContextMenuService class' Manager property if you are doing this in XAML. However, if you are doing this in code, you have to instantiate a ContextMenuManager object and attach it to a control using the ContextMenuService class' static SetManager method.

    In XAML:

    <ig:ContextMenuService.Manager>
        <ig:ContextMenuManager>
            <ig:ContextMenuManager.ContextMenu>
                <!--TODO: Add a xamContextMenu control here-->
            </ig:ContextMenuManager.ContextMenu>
        </ig:ContextMenuManager>
    </ig:ContextMenuService.Manager>
    

    In Visual Basic:

    Dim contextMenuManager1 As New ContextMenuManager()
    ContextMenuService.SetManager(textBox1, contextMenuManager1)
    
    'TODO: Set the ContextMenuManager object's ContextMenu property
    

    In C#:

    ContextMenuManager contextMenuManager1 = new ContextMenuManager();
    ContextMenuService.SetManager(textBox1, contextMenuManager1);
    
    //TODO: Set the ContextMenuManager object's ContextMenu property
    
  6. Create a xamContextMenu control. If you are doing this in code, make sure you set the ContextMenuManager object's ContextMenu property.

    In XAML:

    <ig:XamContextMenu>
        <!--TODO: Add XamMenuItem objects here-->
    </ig:XamContextMenu>
    

    In Visual Basic:

    Dim xamContextMenu1 As New XamContextMenu()
    contextMenuManager1.ContextMenu = xamContextMenu1
    

    In C#:

    XamContextMenu xamContextMenu1 = new XamContextMenu();
    contextMenuManager1.ContextMenu = xamContextMenu1;
    
  7. Add two XamMenuItem objects to the xamContextMenu control. In XAML, you do not have to declare tags for xamContextMenu's Items collection.
    • Set the first XamMenuItem object's Header property to "Font".
    • Set the second XamMenuItem object's Header property to "Formatting".

    In XAML:

    <ig:XamMenuItem Header="Font" />
    <ig:XamMenuItem Header="Formatting" />
    

    In Visual Basic:

    Dim menuItemFont As New XamMenuItem With {.Header = "Font"}
    Dim menuItemFormatting As New XamMenuItem With {.Header = "Formatting"}
    
    xamContextMenu1.Items.Add(menuItemFont)
    xamContextMenu1.Items.Add(menuItemFormatting)
    

    In C#:

    XamMenuItem menuItemFont = new XamMenuItem
    {
        Header = "Font"
    };
    
    XamMenuItem menuItemFormatting = new XamMenuItem
    {
        Header = "Formatting"
    };
    
    xamContextMenu1.Items.Add(menuItemFont);
    xamContextMenu1.Items.Add(menuItemFormatting);
    
  8. Run the application. You can handle the XamMenuItem objects' Click events to add functionality.
Related Topics

About xamContextMenu

Using xamContextMenu