Right-click Context Menu for GraphicLayers

4472
6
03-14-2011 11:30 AM
FrancoisChartrand
New Contributor III
We would like the ability to create a right-click context menu for Graphics from a GraphicLayer.

It seems that we cannot use the .NET Framework ContextMenu class since it requires an UIElement for the PlacementTarget property.

Is there a way to integrate a right-click context menu into my map?

Thanks!
0 Kudos
6 Replies
BrentHoskisson
Occasional Contributor III
This one has become my best friend.  Plus you can get the source code

http://sl4popupmenu.codeplex.com/

Good Luck
0 Kudos
FrancoisChartrand
New Contributor III
I found some ways to do add Context Menus.

The easiest way is to add the Context Menu to the Symbol's ControlTemplate. However, when doing  this, I haven't found a way to retrieve the Graphic object when executing the Context Menu's Command which is sad because you won't be able to give some context to the Command.

The second way is to do it by code by creating your own Graphic class. You won't be able to set the PlacementTarget property because the ArcGIS API doesn't give you access to the corresponding UIElement but it doesn't seem to make any difference since the ContextMenu will show up properly. In addition, by doing it this way, it is very easy to set the CommandParameter to the Graphic object. Here is the code for the second option:

class MyGraphic : ESRI.ArcGIS.Client.Graphic
{
    public MyGraphic()
    {
        MouseRightButtonUp += (sender, e) =>
        {
            ContextMenu contextMenu = BuildContextMenu();
            contextMenu.IsOpen = true;
            e.Handled = true;
        };
    }

    private ContextMenu BuildContextMenu()
    {
        ContextMenu contextMenu = new ContextMenu();
        BuildContextMenuCore(contextMenu);
        return contextMenu;
    }

    protected virtual void BuildContextMenuCore(ContextMenu contextMenu)
    {
        contextMenu.Items.Add(new MenuItem
        {
            Header = "Properties",
            CommandParameter = this,
            Command = Commands.Map.Properties
        });
    }
}


Please let me know if you find a better way...
0 Kudos
AnandKirti
New Contributor

How I load this ContextMenu when user make a right Click? Any code snippet? Please share

0 Kudos
BrentHoskisson
Occasional Contributor III
The Graphics Layer has a MouseRightButtonDown.  It doesn't mean you have to use the graphic as the UI Element.  The Map can be the UI Element.

I don't know how the context menu you are using works, but, with mine, I can:

grab the graphics information from the EventArgs and set up the popup accordingly,
set the location of the context also from the event args and
open the popup on the Map control - it will come up over the graphic and the user wont care.

Good Luck.
0 Kudos
FrancoisChartrand
New Contributor III
Thank you Brent for your help. You are right: I could also use the MouseRightButtonUp from the Graphics Layer and use the EventArgs to retrieve the graphics information.

However, I can't use the Map itself as the UIElement for the PlacementTarget property. The PlacementTarget is the element relative to which the ContextMenu is positioned when it opens. Setting it to the map wouldn't make sense.

I'm using the standard ContextMenu class from the .NET Framework. I should also mention that I'm developing a WPF application, not Silverlight.
0 Kudos
AnandKirti
New Contributor

How I load this ContextMenu when user make a right Click? Any code snippet? Please share

0 Kudos