Select to view content in your preferred language

Context Menu failure for graphics

925
2
02-04-2013 05:25 PM
Labels (1)
ChrisWheeler
New Contributor
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;
    }
  }
}
0 Kudos
2 Replies
JustinShepard
Deactivated User
Did you get the context menu working for features? I'm looking to set a context menu on the right-click handler.
0 Kudos
AnttiKajanus1
Deactivated User
Here is simple sample how to get started with ContextMenu.

Xaml:
<esri:Map x:Name="_map" UseAcceleratedDisplay="True">
    <esri:Map.ContextMenu>
        <ContextMenu x:Name="_contextMenu"
                     Closed="_contextMenu_Closed">
            <ContextMenu.ItemsSource>
                <CompositeCollection>
                    <MenuItem Header="Some general fuctionality" Click="MenuItem_Click_1" />
                    <MenuItem Header="Show geometry (if had graphic)" Click="MenuItem_Click" IsEnabled="False"/>
                </CompositeCollection>
            </ContextMenu.ItemsSource>
        </ContextMenu>
    </esri:Map.ContextMenu>
    <esri:ArcGISLocalDynamicMapServiceLayer ID="USA" Path="C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.2\sdk\samples\data\MPKs\USCitiesStates.mpk"/>
    <esri:GraphicsLayer ID="Example Graphic" MouseRightButtonUp="GraphicsLayer_MouseRightButtonUp">
        <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>



Code behind:
using System.Windows;
using ESRI.ArcGIS.Client;
using System.Windows.Controls;
using System.Linq;


namespace ArcGISWpfApplication1
{


    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        Graphic _graphic;


        private void GraphicsLayer_MouseRightButtonUp(object sender, GraphicMouseButtonEventArgs e)
        {
            (_contextMenu.Items[1] as MenuItem).IsEnabled = e.Graphic != null ? true : false;
            if (e.Graphic == null)
            {
                return;
            }


            var layer = _map.Layers.OfType<GraphicsLayer>().First();
            _graphic = e.Graphic;
        }


        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(_graphic.Geometry.ToJson());
            _graphic = null;
            (_contextMenu.Items[1] as MenuItem).IsEnabled = false;
        }


        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Note that points in USA are not graphics, they are rendered in image on dynamic layer. There is red dot on 0,0 - click that");
        }

        private void _contextMenu_Closed(object sender, RoutedEventArgs e)
        {
            _graphic = null;
            (_contextMenu.Items[1] as MenuItem).IsEnabled = false;
        }
    }
}
0 Kudos