Retrieve the Clicked Item When Opening a RadContextMenu
Environment
Product Version | 2023.3.1114 |
Product | RadContextMenu for WPF |
Description
Retrieve the clicked item when the RadContextMenu is opened.
Solution
You can derive from the RadContextMenu
class and add an additional dependency property.
Deriving from the RadContextMenu class and adding an additional dependency property
public class ExtendedContextMenu : RadContextMenu
{
public static readonly DependencyProperty ClickedItemProperty =
DependencyProperty.Register("ClickedItem", typeof(FrameworkElement), typeof(ExtendedContextMenu), new PropertyMetadata(null));
public FrameworkElement ClickedItem
{
get { return (FrameworkElement)GetValue(ClickedItemProperty); }
set { SetValue(ClickedItemProperty, value); }
}
}
Public Class ExtendedContextMenu
Inherits RadContextMenu
Public Shared ReadOnly ClickedItemProperty As DependencyProperty = DependencyProperty.Register("ClickedItem", GetType(FrameworkElement), GetType(ExtendedContextMenu), New PropertyMetadata(Nothing))
Public Property ClickedItem As FrameworkElement
Get
Return CType(GetValue(ClickedItemProperty), FrameworkElement)
End Get
Set(ByVal value As FrameworkElement)
SetValue(ClickedItemProperty, value)
End Set
End Property
End Class
To assign a value to the dependency property, override the OnOpened
method and call the GetClickedElement
method. This method will allow you to retrieve the first element of the given type at the click point coordinates.
In the following example, the custom RadContextMenu is used in the RadTreeListView
control and a reference to the clicked TreeListViewRow
instance is retrieved in the OnOpened method.
Retrieving the clicked TreeListViewRow instance inside the OnOpened method of the custom RadContextMenu
public class ExtendedContextMenu : RadContextMenu
{
public static readonly DependencyProperty ClickedItemProperty =
DependencyProperty.Register("ClickedItem", typeof(FrameworkElement), typeof(ExtendedContextMenu), new PropertyMetadata(null));
public FrameworkElement ClickedItem
{
get { return (FrameworkElement)GetValue(ClickedItemProperty); }
set { SetValue(ClickedItemProperty, value); }
}
protected override void OnOpened(RadRoutedEventArgs e)
{
base.OnOpened(e);
this.ClickedItem = null;
this.ClickedItem = this.GetClickedElement<TreeListViewRow>();
}
}
Public Class ExtendedContextMenu
Inherits RadContextMenu
Public Shared ReadOnly ClickedItemProperty As DependencyProperty = DependencyProperty.Register("ClickedItem", GetType(FrameworkElement), GetType(ExtendedContextMenu), New PropertyMetadata(Nothing))
Public Property ClickedItem As FrameworkElement
Get
Return CType(GetValue(ClickedItemProperty), FrameworkElement)
End Get
Set(ByVal value As FrameworkElement)
SetValue(ClickedItemProperty, value)
End Set
End Property
Protected Overrides Sub OnOpened(ByVal e As RadRoutedEventArgs)
MyBase.OnOpened(e)
Me.ClickedItem = Nothing
Me.ClickedItem = Me.GetClickedElement(Of TreeListViewRow)()
End Sub
End Class