Edit this page

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.

[XAML] Example 1: Attach a RadContextMenu

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

[C#] Example 1: Attach a RadContextMenu

RadContextMenu radContextMenu = new RadContextMenu();
RadContextMenu.SetContextMenu(this.textBox, radContextMenu);

[VB.NET] Example 1: Attach a 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.

[XAML] 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>

[C#] 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);

[VB.NET] Example 2: Add menu items

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.

[C#] Example 3: Get clicked element

RadTreeViewItem item = radContextMenu.GetClickedElement<RadTreeViewItem>();

[VB.NET] Example 3: Get clicked element

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