Select to view content in your preferred language

Error in displaying/highlighting selected graphics ....

510
2
02-20-2013 06:30 AM
YurongTan
Regular Contributor
I getting an error for the following and have no clue what it means.

The error is a run time error: "Speficied argument was out of the range of valid values.  Parameter name: Graphic is already associated with another layer."

The error points to this line:   graphicsLayerTEMP.Graphics.Insert(0, thisgraphic);

Thanks in advance for any help.



private void FindDetailsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
               DataGrid dataGrid = sender as DataGrid;
int selectedIndex = dataGrid.SelectedIndex;
Graphic thisgraphic = (Graphic)FindDetailsDataGridOHI.SelectedItem;

if (selectedIndex > -1)
{
     thisgraphic.Symbol = LayoutRoot.Resources["griddataPointRendererTEMP"] as ESRI.ArcGIS.Client.Symbols.Symbol;
  // GridDataSelectedGraphicTEMP is a separate graphic layer just to highlight the selected point
  GraphicsLayer graphicsLayerTEMP = MyMap.Layers["GridDataSelectedGraphicTEMP"] as GraphicsLayer;

  thisgraphic.Geometry.SpatialReference = MyMap.SpatialReference;
  graphicsLayerTEMP.ClearGraphics();
  graphicsLayerTEMP.Graphics.Insert(0, thisgraphic);
 
  if ( myCursorAt == "TABLE" )  MyMap.PanTo(graphicsLayerTEMP.FullExtent.Extent);
}
}
0 Kudos
2 Replies
JohanCarlsson
Regular Contributor
A graphic can only be associated with one layer. You have to clone your graphic object and then add it to your layer.

Graphic clonedGraphic = new Graphic
{ 
    Symbol = yourOldGraphic.Symbol,
    Geometry = Geometry.Clone(yourOldGraphic.Geometry),
};

foreach (var attribute in yourOldGraphic.Attributes)
    clonedGraphic.Attributes.Add(attribute);

layer.Graphics.Add(clonedGraphic);


Hope this helps.
0 Kudos
YurongTan
Regular Contributor
A graphic can only be associated with one layer. You have to clone your graphic object and then add it to your layer.

Graphic clonedGraphic = new Graphic
{ 
    Symbol = yourOldGraphic.Symbol,
    Geometry = Geometry.Clone(yourOldGraphic.Geometry),
};

foreach (var attribute in yourOldGraphic.Attributes)
    clonedGraphic.Attributes.Add(attribute);

layer.Graphics.Add(clonedGraphic);


Hope this helps.




Thanks a lot for the reply, but it seems that I couldn't make it work.  The following code works fine except
for the mentioned error at either one of these two lines:
   //flashPointGraphicsLayer.Graphics.Add(thisgraphic);
   //flashPointGraphicsLayer.Graphics.Insert(0,thisgraphic);
If I comment them out, the MyMap pans correctly as different record is selected. 

private HighlightCorrespondingPointOnMapForSelectedRecord()
{
//Below is a record selected/hightlighted in the DataGrid table.
Graphic thisgraphic = (Graphic)FindDetailsDataGridOHI.SelectedItem;
if (thisgraphic != null )
{
    thisgraphic.Symbol = LayoutRoot.Resources["flashPointRenderer"] as ESRI.ArcGIS.Client.Symbols.Symbol;
    thisgraphic.Geometry.SpatialReference = MyMap.SpatialReference;
    GraphicsLayer flashPointGraphicsLayer = MyMap.Layers["transitionPointGraphicTemp"] as GraphicsLayer;
    flashPointGraphicsLayer.ClearGraphics();

    //flashPointGraphicsLayer.Graphics.Add(thisgraphic);
    //flashPointGraphicsLayer.Graphics.Insert(0,thisgraphic);
    MyMap.PanTo(thisgraphic.Geometry);
}
}
0 Kudos