ag_cwheeler

Context Menu failure for graphics

Discussion created by ag_cwheeler on Feb 4, 2013
Latest reply on Mar 31, 2014 by akajanus-esristaff
Your context menu handling within the Graphic.MouseLeftButtonDown event throws an unhandled exception somewhere I can't reach it.  I'd really like to be able to use context menus here, but any attempt to trigger them crashes your code.  Also, your symbol servers aren't working.

source XAML:

<Window x:Class="agsrtcanvas.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
        Title="MainWindow" Height="350" Width="525">
  <DockPanel LastChildFill="True" x:Name="layoutRoot">
    <Canvas>
      <esri:Map x:Name="_map" Height="350" Width="525" UseAcceleratedDisplay="True">
        <esri:ArcGISLocalDynamicMapServiceLayer ID="USA" Path="C:\Program Files (x86)\ArcGIS SDKs\WPF1.0\SDK\Samples\Data\MPKs\USCitiesStates.mpk"/>
        <esri:GraphicsLayer ID="Example Graphic">
                <esri:Graphic x:Name="testGraphic">
                    <esri:Graphic.Symbol>
                        <esri:SimpleMarkerSymbol Style="Circle" Color="Red"/>
                    </esri:Graphic.Symbol>
                    <esri:Graphic.Geometry>
                        <esri:MapPoint  X="0" Y="0" >
                            <esri:MapPoint.SpatialReference>
                                <esri:SpatialReference WKID="102100"/>
                            </esri:MapPoint.SpatialReference>
                        </esri:MapPoint>                   
                        </esri:Graphic.Geometry>                 
                </esri:Graphic>
            </esri:GraphicsLayer>
      </esri:Map>
      <Rectangle Height="10" Width="10" Fill="Black" Canvas.Left="100" Canvas.Top="100" Name="contextMenuHost" MouseDown="contextMenuHost_MouseDown"  >
        <ContextMenuService.ContextMenu>
          <ContextMenu x:Name="contextMenu">
            <MenuItem Header="Edit">
            </MenuItem>
            <Separator></Separator>
            <MenuItem Header="Delete">
            </MenuItem>
          </ContextMenu>
        </ContextMenuService.ContextMenu>
      </Rectangle>
    </Canvas>
  </DockPanel>
</Window>

Source CS:

using System.Windows;
using ESRI.ArcGIS.Client;

namespace agsrtcanvas
{

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      // License setting and ArcGIS Runtime initialization is done in Application.xaml.cs.

      InitializeComponent();
      testGraphic.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(testGraphic_MouseLeftButtonDown);
    }

    void testGraphic_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
      contextMenu.IsOpen = true;
    }

    private void contextMenuHost_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
      contextMenu.IsOpen = true;
    }
  }
}

Outcomes