Select to view content in your preferred language

Graphics Layer and ContextMenu

2567
4
08-04-2010 09:20 AM
StephenDavis
Emerging Contributor
Is there a way to attach a context menu in the new 2.0 API/Silverlight 4.0 to a graphic in a graphics layer or the graphics layer itself.  It is a dependency object, so it should work, however when I try to add one dynamically, it doesn't do anything.  Any help would be appreciated.

Something like:

            ContextMenu CM = new ContextMenu();
            CM.Items.Add(new MenuItem() { Header = "Test" });
            (CM.Items[0] as MenuItem).Click += new RoutedEventHandler(Item_Click);
            ContextMenuService.SetContextMenu(GraphicsLayer, CM);

Or:

            ContextMenu CM = new ContextMenu();
            CM.Items.Add(new MenuItem() { Header = "Test" });
            (CM.Items[0] as MenuItem).Click += new RoutedEventHandler(Item_Click);
            ContextMenuService.SetContextMenu(GraphicsLayer.Graphics.First(), CM);

Thanks!
Stephen
0 Kudos
4 Replies
JenniferNery
Esri Regular Contributor
How about adding this to the graphic's attributes (IDictionary<string, object>)? You are looking for a temporary storage for the ContextMenu, right?

Jennifer
0 Kudos
StephenDavis
Emerging Contributor
No, I am trying to assign the context menu object to the graphic object so that when you right click on the graphic, the context menu shows up.  Either that or assign it to the graphics layer so that you get the same context menu no matter what graphic you right click on.  I have tried both, and neither have worked for me.
0 Kudos
DanDulan
Emerging Contributor
I know is not very elegant but you can try this:
add a context menu to the map, set the visibility to colapsed and then on the GraphicsLayer_MouseRightButtonDown event set it back to visible.


Thanks,
Dan
0 Kudos
StephenDavis
Emerging Contributor
Unfortunately, that won't work for me as I have a different context menu on the map itself.  I guess I could set the visibility on each MenuItem individually but I'm hoping to find a more appropriate solution.
0 Kudos