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.
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.
Create a Microsoft® WPF™ application.
Add the following NuGet package to your application:
Infragistics.WPF.Menus
For more information on setting up the NuGet feed and adding NuGet packages, you can take a look at the following documentation: NuGet Feeds.
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;
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
}
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
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;
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);
Run the application. You can handle the XamMenuItem objects' Click events to add functionality.