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.