Handle Item Clicks

There are two ways to handle a click on an item:

Handle the Click event of the RadMenuItem

Handling the Click event of each item is the straight-forward way. But it has some disadvantages:

  • You have to attach an event handler to each item. This makes the code harder to maintain.

  • It is not suitable when having dynamic items.

If the RadMenuItem is in the role of a header (has child items), the ItemClick event won't be raised unless the NotifyOnHeaderClick property is set to True.

Here is an example of an event handler attached to the Click event and how to get the instance of the clicked item.

XAML

<telerik:RadContextMenu>
    <telerik:RadMenuItem Header="Item 1"
                                   Click="RadMenuItem_Click" />
    <telerik:RadMenuItem Header="Item 2"
                                   Click="RadMenuItem_Click" />
    <telerik:RadMenuItem Header="Item 3"
                                   Click="RadMenuItem_Click" />
</telerik:RadContextMenu>

C#

private void RadMenuItem_Click( object sender, RadRoutedEventArgs e )
{
    RadMenuItem item = sender as RadMenuItem;
    //implement the logic regarding the instance here.
}

VB.NET

Private Sub RadMenuItem_Click(sender As Object, e As RadRoutedEventArgs)
 Dim item As RadMenuItem = TryCast(sender, RadMenuItem)
 'implement the logic regarding the instance here.
End Sub

Handle the ItemClick event of the RadContextMenu

Handling the ItemClick event of the RadContextMenu gives you more flexibility, as it fires each time a child menu item is clicked. This approach is the most suitable when having a dynamic data scenario.

If the RadMenuItem is in the role of a header (has child items), the ItemClick event won't be raised unless the NotifyOnHeaderClick property is set to True.

Here is an example of an event handler attached to the ItemClick event and how to get the instance of the clicked item.

XAML

<telerik:RadContextMenu ItemClick="radContextMenu_ItemClick">
    <telerik:RadMenuItem Header="Item 1" />
    <telerik:RadMenuItem Header="Item 2" />
    <telerik:RadMenuItem Header="Item 3" />
</telerik:RadContextMenu>

C#

private void radContextMenu_ItemClick( object sender, RadRoutedEventArgs e )
{
    RadMenuItem item = e.OriginalSource as RadMenuItem;
    //implement the logic regarding the instance here.
}

VB.NET

Private Sub radContextMenu_ItemClick(sender As Object, e As RadRoutedEventArgs)
 Dim item As RadMenuItem = TryCast(e.OriginalSource, RadMenuItem)
 'implement the logic regarding the instance here.
End Sub

See Also