Skip to content

Files

Latest commit

Nov 28, 2023
a71370d · Nov 28, 2023

History

History
115 lines (95 loc) · 4.24 KB

kb-contextmenu-retrieve-the-clicked-element.md

File metadata and controls

115 lines (95 loc) · 4.24 KB
title description page_title type slug position tags ticketid res_type
Retrieve the Clicked Item When Opening a RadContextMenu
This article shows how to retrieve the clicked item when opening the RadContextMenu
Retrieve the Item That is Clicked on When a RadContextMenu is Opened
troubleshooting
kb-contextmenu-retrieve-clicked-item-when-opening
0
1613605
kb

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.

[C#] Deriving from the RadContextMenu class and adding an additional dependency property

{{region kb-contextmenu-retrieve-clicked-item-when-opening-0}} 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); }
    }
}

{{endregion}}

[VB.NET] Deriving from the RadContextMenu class and adding an additional dependency property

{{region kb-contextmenu-retrieve-clicked-item-when-opening-1}} 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

{{endregion}}

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.

[C#] Retrieving the clicked TreeListViewRow instance inside the OnOpened method of the custom RadContextMenu

{{region kb-contextmenu-retrieve-clicked-item-when-opening-2}} 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>();
    }
}

{{endregion}}

[VB.NET] Retrieving the clicked TreeListViewRow instance inside the OnOpened method of the custom RadContextMenu

{{region kb-contextmenu-retrieve-clicked-item-when-opening-3}} 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

{{endregion}}