New to Telerik UI for WPF? Download free 30-day trial

RadContextMenu Usage

This article will make you familiar with the basics around the usage of the RadContextMenu. The following topics will be discussed:

Attach a RadContextMenu to a Control

To attach a RadContextMenu to a control, you have to set the instance of it to the RadContextMenu.ContextMenu attached property.

Example 1: Attach a RadContextMenu

<TextBox x:Name="textBox" 
         Width="200" 
         VerticalAlignment="Top"> 
    <telerik:RadContextMenu.ContextMenu> 
        <telerik:RadContextMenu> 
        </telerik:RadContextMenu> 
    </telerik:RadContextMenu.ContextMenu> 
</TextBox> 

Example 1: Attach a RadContextMenu

RadContextMenu radContextMenu = new RadContextMenu(); 
RadContextMenu.SetContextMenu(this.textBox, radContextMenu); 
Dim radContextMenu As New RadContextMenu() 
radContextMenu.SetContextMenu(Me.textBox, radContextMenu) 

Add Menu Items to the RadContextMenu

The class that represents the individual menu items is Telerik.Windows.Controls.RadMenuItem, located in the Telerik.Windows.Controls.Navigation assembly.

To learn more about the RadMenuItem control, please take a look at the RadMenu's documentation.

The RadContextMenu accepts RadMenuItems as child items. Here is a sample declaration of several child menu items.

Example 2: Add Menu Items

<TextBox x:Name="textBox1" 
         Width="200" 
         VerticalAlignment="Top"> 
    <telerik:RadContextMenu.ContextMenu> 
        <telerik:RadContextMenu> 
            <telerik:RadMenuItem Header="Copy" /> 
            <telerik:RadMenuItem Header="Paste" /> 
            <telerik:RadMenuItem Header="Cut" /> 
        </telerik:RadContextMenu> 
    </telerik:RadContextMenu.ContextMenu> 
</TextBox> 

Example 2: Add Menu Items

RadContextMenu radContextMenu = new RadContextMenu(); 
RadMenuItem copyItem = new RadMenuItem(); 
copyItem.Header = "Copy"; 
radContextMenu.Items.Add(copyItem); 
RadMenuItem pasteItem = new RadMenuItem(); 
pasteItem.Header = "Paste"; 
radContextMenu.Items.Add(pasteItem); 
RadMenuItem cutItem = new RadMenuItem(); 
cutItem.Header = "Cut"; 
radContextMenu.Items.Add(cutItem); 
RadContextMenu.SetContextMenu(this.textBox, radContextMenu); 
Dim radContextMenu As New RadContextMenu() 
Dim copyItem As New RadMenuItem() 
copyItem.Header = "Copy" 
radContextMenu.Items.Add(copyItem) 
Dim pasteItem As New RadMenuItem() 
pasteItem.Header = "Paste" 
radContextMenu.Items.Add(pasteItem) 
Dim cutItem As New RadMenuItem() 
cutItem.Header = "Cut" 
radContextMenu.Items.Add(cutItem) 
radContextMenu.SetContextMenu(Me.textBox, radContextMenu) 

Get the Clicked Element

When an element gets clicked and the RadContextMenu appears, you might need to get the instance of that element. This can be very useful when you have an ItemsControl that integrates with the RadContextMenu, as you can get the instance of the item that has been clicked. For this purpose, the RadContextMenu exposes the GetClickedElement<T>() method. This will return the first element of type T in the VisualTree, that is part of the route of the RadContextMenu's triggering event.

For example, if you have a RadTreeView with a RadContextMenu attached, and you want to get the instance of the RadTreeViewItem, which has been right-clicked, you can use the following lines of code.

Example 3: Get clicked element

RadTreeViewItem item = radContextMenu.GetClickedElement<RadTreeViewItem>(); 
Dim item As RadTreeViewItem = radContextMenu.GetClickedElement(Of RadTreeViewItem)() 

A more complex example scenario can be found in the Use RadContextMenu with a RadGridView topic and in the Select the Clicked Item of a RadTreeView topic.

See Also

In this article