New to Telerik UI for WinFormsStart a free 30-day trial

Modifying the Default Context Menu

Updated over 6 months ago

RadTreeView provides a convenient API that will allow you to modify its context menu. The ContextMenuOpening event is the place where the modifications should be done. The event arguments of this event expose several useful properties:

  • Cancel: If you set this property to true, the default context menu will not be shown.

  • Menu: This property returns the default context menu that is going to be shown.

  • Node: The node for which the context menu is invoked. This node is a data node of type RadTreeNode. The following Node properties are useful in such scenarios:

    • Text: Returns the text of the node. This comes in handy when you want to conditionally modify the context menu depending on the text of the node.

    • Level: This property returns an integer that indicates the level of the node in the three hierarchy. The level index is zero-based.

    • DataBoundItem: If RadTreeView is data-bound, each data node (of type RadTreeNode) has an associated DataBoundItem. DataBoundItem is the object of the underlying data source for which a RadTreeNode is created. You may need to modify the context menu depending on the data that you can extract from the DataBoundItem object.

  • TreeViewElement: This is the main element of the RadTreeView control.

  • TreeView: TreeView is the RadTreeView control for which the ContextMenuOpening is fired.

Accessing default menu items

Depending on your scenario, you may need to show/hide/enable/disable some items of the default context menu. But how to determine in code which is the item that you want to modify? The items of the default context menu can be recognized by their Name property. This gives you freedom when you want to apply a custom TreeViewLocalizationProvider, because the Name value is not changed, hence your code. Here are the items of the default context menu and their respective Name values:

TextName
ExpandExpandCollapse
CollapseExpandCollapse
NewNew
EditEdit
DeleteDelete
CutCut
CopyCopy
PastePaste

Example: Removing items depending on nodes' Level and DataBoundItem

Let's assume that we have a RadTreeView bound to a DataTable of Files and Folders. This data-table has ID, Name, IsSystemItem and ParendID fields. The records in this table refer to each other by the ID-ParentID relation. The nodes of RadTreeView will show the Name of the records.

Let's now assume that we want to hide the Delete menu item for nodes of those records that have their IsSystemItem set to true. Additionally, we want to hide the New menu item for nodes that exist at the top level of the tree.

Here is a table of the data that we are going to pass to RadTreeView:

IDNameIsSystemItemParentID
0My Computertrue-1
1C:|true0
2D:|true0
3Program Filestrue1
4Microsoftfalse3
5Telerikfalse3
6WINDOWStrue1

Following our requirements, we prepare the this implementation:

C#
void radTreeView1_ContextMenuOpening1(object sender, Telerik.WinControls.UI.TreeViewContextMenuOpeningEventArgs e)
{
    DataRowView rowView = (DataRowView)e.Node.DataBoundItem;
    DataRow row = rowView.Row;
    for (int i = e.Menu.Items.Count - 1; i >= 0; i--)
    {
        if (e.Menu.Items[i].Name == "Delete")
        {
            if ((bool)row.ItemArray[2] == true)
            {
                e.Menu.Items.Remove(e.Menu.Items[i]);
            }
        }
        if (e.Menu.Items[i].Name == "New")
        {
            if (e.Node.Level == 0)
            {
                e.Menu.Items.Remove(e.Menu.Items[i]);
            }
        }
    }         
}

And here is the result:

WinForms RadTreeView Context Menu

See Also