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.
Note |
|---|
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.
CopyXAML
<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>
CopyC#
private void RadMenuItem_Click( object sender, RadRoutedEventArgs e )
{
RadMenuItem item = sender as RadMenuItem;
}
CopyVB.NET
Private Sub RadMenuItem_Click(sender As Object, e As RadRoutedEventArgs)
Dim item As RadMenuItem = TryCast(sender, RadMenuItem)
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.
Note |
|---|
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.
CopyXAML
<telerik:RadContextMenu ItemClick="radContextMenu_ItemClick">
<telerik:RadMenuItem Header="Item 1" />
<telerik:RadMenuItem Header="Item 2" />
<telerik:RadMenuItem Header="Item 3" />
</telerik:RadContextMenu>
CopyC#
private void radContextMenu_ItemClick( object sender, RadRoutedEventArgs e )
{
RadMenuItem item = e.OriginalSource as RadMenuItem;
}
CopyVB.NET
Private Sub radContextMenu_ItemClick(sender As Object, e As RadRoutedEventArgs)
Dim item As RadMenuItem = TryCast(e.OriginalSource, RadMenuItem)
End Sub
See Also