I am creating a right click menu using XamContextMenu that I am attaching to XamMenuItems in a XamMenu. The menu displays and the click even fires. But, how can I determine what object the context menu is attached to?
I need to get a reference to the object which was right-clicked on to bring up the context menu.
thanks,
BOb
Hi,
You can hook up to the XamContextMenu's Opening / Opened event and from the event handler obtain a reference to the XamMenuItem that was clicked by calling the GetClickedElement() off the event arguments object, here is a sample code:
<ig:ContextMenuService.Manager> <ig:ContextMenuManager OpenMode="RightClick"> <ig:ContextMenuManager.ContextMenu> <ig:XamContextMenu x:Name="mnuContext" Opening="mnuContext_Opening"> <ig:XamMenuItem x:Name="mnuName" Header="Some Header"/> </ig:XamContextMenu> </ig:ContextMenuManager.ContextMenu> </ig:ContextMenuManager> </ig:ContextMenuService.Manager>
private void mnuContext_Opening(object sender, OpeningEventArgs e) { var item = e.GetClickedElements<XamMenuItem>().SingleOrDefault(); // do something with the item }
HTH,
How does that fit MVVM pattern ? if i want to process the event to main View Model.
Excellent.. thanks. I had to mod a small bit, there was no "SingleOrDefault() on the collection. So, I just use [0]. But other than that, it works a treat.
I thought I had tried this but didn't see that Event... I must have been looking at the menu item and not the menu itself.