# Selecting the Clicked Item of a RadTreeView

## Environment

<table>
<tbody>
<tr>
<th scope="row">Product Version</th>
<td>2026.1.415</td>
</tr>
<tr>
<th scope="row">Product</th>
<td>RadContextMenu for WPF</td>
</tr>
</tbody>
</table>

## Description

Select the RadTreeViewItem that was clicked when the RadContextMenu opens.

## Solution

Handle the RadContextMenu Opened event, get the clicked RadTreeViewItem, and assign it to the RadTreeView\.SelectedItem property.

This tutorial will show you how to select the item that was clicked, while opening the **RadContextMenu**. In order to achieve this, you have to do the following things:

* Handle the **Opened** event of the **RadContextMenu**

* Get an instance of the clicked **RadTreeViewItem**

* Set the **SelectedItem** of the **RadTreeView**

Before starting, here is a sample **RadTreeView** with a sample **RadContextMenu** attached.

**Attach RadContextMenu to RadTreeView**

```XAML
<telerik:RadTreeView xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Name="radTreeView">
    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu x:Name="radContextMenu">
            <telerik:RadMenuItem Header="Menu Option 1" />
            <telerik:RadMenuItem Header="Menu Option 2" />
            <telerik:RadMenuItem Header="Menu Option 3" />
        </telerik:RadContextMenu>
    </telerik:RadContextMenu.ContextMenu>
    <telerik:RadTreeViewItem Header="Category1">
        <telerik:RadTreeViewItem Header="Product1" />
        <telerik:RadTreeViewItem Header="Product2" />
        <telerik:RadTreeViewItem Header="Product3" />
    </telerik:RadTreeViewItem>
    <telerik:RadTreeViewItem Header="Category2" />
    <telerik:RadTreeViewItem Header="Category3" />
    <telerik:RadTreeViewItem Header="Category4">
        <telerik:RadTreeViewItem Header="Product A" />
        <telerik:RadTreeViewItem Header="Product B" />
        <telerik:RadTreeViewItem Header="Product C" />
    </telerik:RadTreeViewItem>
    <telerik:RadTreeViewItem Header="Category5" />
</telerik:RadTreeView>
```

To handle the **Opened** event attach an event handler to it.

**Handle the RadContextMenu Opened Event**

```XAML
<telerik:RadContextMenu xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                        x:Name="radContextMenu"
                        Opened="RadContextMenu_Opened">
    <telerik:RadMenuItem Header="Menu Option 1" />
    <telerik:RadMenuItem Header="Menu Option 2" />
    <telerik:RadMenuItem Header="Menu Option 3" />
</telerik:RadContextMenu>
```

**Define the Opened Event Handler in C#**

```C#
private void RadContextMenu_Opened( object sender, RoutedEventArgs e )
{
}
```

In it get the instance of the clicked **RadTreeViewItem** by calling the **GetClickedElement\<T>()** method of the **RadContextMenu**.

**Get the Clicked RadTreeViewItem in C#**

```C#
private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
{
    RadTreeViewItem item = this.radContextMenu.GetClickedElement<RadTreeViewItem>();
}
```

The last thing to do is to set the **SelectedItem** property of the **RadTreeView** to the **instance** of the **RadTreeView** item that has been clicked.

> note If you are having a dynamic data scenario, where the **RadTreeView** is bound to a collection, you have to set the **SelectedItem** property to the **DataContext** of the clicked **RadTreeViewItem**.

**Select the Clicked Item in C#**

```C#
private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
{
    RadTreeViewItem item = this.radContextMenu.GetClickedElement<RadTreeViewItem>();
    if (item != null)
    {
        this.radTreeView.SelectedItem = item;
    }
}
```

The selected item is now available through the `SelectedItem` property of the tree view.

## See Also

* [Context Menu Owner](https://www.telerik.com/products/wpf/documentation/controls/radcontextmenu/context-menu-owner)

* [Use RadContextMenu with a RadGridView](https://www.telerik.com/products/wpf/documentation/knowledge-base/kb-contextmenu-use-with-radgridview)

* [Create Menu Button with RadContextMenu and ToggleButton](https://www.telerik.com/products/wpf/documentation/knowledge-base/kb-contextmenu-create-menu-button-with-togglebutton)

* [Use Commands with the RadContextMenu](https://www.telerik.com/products/wpf/documentation/knowledge-base/kb-contextmenu-use-commands)

* [Handle Item Clicks](https://www.telerik.com/products/wpf/documentation/knowledge-base/kb-contextmenu-handle-item-clicks)
