Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
100
Intercept Field from MenuItem
posted
Hi.
In a Xamdatagrid I added a menuitem in HeaderLabelArea from the Code behind. This is code:


Dim MyMenuitem As New MenuItem
MyMenuitem.Header = "Move to first"
 
AddHandler MyMenuitem.Click, AddressOf Me.MenuItem_Click
 
Dim MyContextMenu As New ContextMenu
MyContextMenu.Items.Add(MyMenuitem)
 
MyMenuitem.CommandParameter = MyContextMenu
Dim MyStyleForHeaderLabel As New Style(GetType(HeaderLabelArea))
MyStyleForHeaderLabel.Setters.Add(New Setter(ContextMenuProperty, MyContextMenu))
MyXamdatagrid.FieldLayoutSettings.HeaderLabelAreaStyle = MyStyleForHeaderLabel


But now I can not intercept what column the menuitem has been opened so that I can do the operation

Can you help me.

Thank you so much

  • 16495
    Verified Answer
    Offline posted

    Hello Francesco,

    You can add the ContextMenu to LabelPreseter instead of HeaderLabelArea. This way you will be able to use ContextMenu's PlacementTarget property, which returns the UIElement relative to which the menu is positioned when it was clicked, for example:

    Dim MyMenuitem As New MenuItem
    MyMenuitem.Header = "Move to first"
     
    AddHandler MyMenuitem.Click, AddressOf Me.MenuItem_Click
     
    Dim MyContextMenu As New ContextMenu
    MyContextMenu.Items.Add(MyMenuitem)
     
    MyMenuitem.CommandParameter = MyContextMenu
    Dim MyStyleForHeaderLabel As New Style(GetType(LabelPresenter))
    MyStyleForHeaderLabel.Setters.Add(New Setter(ContextMenuProperty, MyContextMenu))
    MyXamdatagrid.FieldSettings.LabelPresenterStyle= MyStyleForHeaderLabel

    Private Sub MyMenuitem_Click(sender As Object, e As RoutedEventArgs)
     Dim lbl As LabelPresenter= TryCast(TryCast(e.OriginalSource, MenuItem).Parent, ContextMenu).PlacementTarget as LabelPresenter
    End Sub

    Let me know if you have any questions.

    Sincerely,
    Zhivko
    Associate Software Developer