Edit this page

Working with the RadContextMenu

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

Attaching a RadContextMenu to a Control

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

XAML

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

C#

public Default_Cs()
{
    InitializeComponent();
    RadContextMenu radContextMenu = new RadContextMenu();
    RadContextMenu.SetContextMenu( this.textBox, radContextMenu );
}

VB.NET

Public Sub New()
    InitializeComponent()
    Dim radContextMenu As New RadContextMenu()
    radContextMenu.SetContextMenu(Me.textBox, radContextMenu)
End Sub

Adding Menu Items to the RadContextMenu

The class that represents the menu item 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 help content.

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

XAML

<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#

public Default_Cs()
{
    InitializeComponent();
    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

Public Sub New()
    InitializeComponent()
    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)
End Sub

Getting 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#

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

VB.NET

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