ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / GraphicsLayerExtensions Class / GetSelectedElements Method
Example

In This Topic
    GetSelectedElements Method (GraphicsLayerExtensions)
    In This Topic
    Gets the currently selected elements within the GraphicsLayer collection.
    Syntax
    public static IReadOnlyList<Element> GetSelectedElements( 
       GraphicsLayer graphicsLayer
    )
    Public Shared Function GetSelectedElements( _
       ByVal graphicsLayer As GraphicsLayer _
    ) As IReadOnlyList(Of Element)

    Parameters

    graphicsLayer

    Return Value

    Remarks
    The map view displaying the graphics layer should be initialized.
    Example
    Remove Graphic elements
    //on the QueuedTask      
    graphicsLayer.RemoveElements(graphicsLayer.GetSelectedElements());
    Un-Select Graphic elements
    var graphicsLayer = MapView.Active.Map.GetLayersAsFlattenedList()
                        .OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();
    if (graphicsLayer == null)
      return;
    //unselect the first element in the currently selected elements
    var elem = graphicsLayer.GetSelectedElements().FirstOrDefault();
    QueuedTask.Run( () => {
      if (elem != null)
        //Unselect one element
        graphicsLayer.UnSelectElement(elem);
    
      //unselect all elements
      graphicsLayer.UnSelectElements();
      //equivalent to
      graphicsLayer.ClearSelection();
    });
    Subscribe to ElementSelectionChangedEvent
    ArcGIS.Desktop.Layouts.Events.ElementEvent.Subscribe((args) => {
      //check if the container is a graphics layer - could be a Layout (or even map view)
      if (args.Container is ArcGIS.Desktop.Mapping.GraphicsLayer graphicsLayer)
      {
        //get the total selection count for the container
        var count = args.Elements.Count();
        //Check count - could have been an unselect or clearselect
        if (count > 0)
        {
          //this is a selection or add to selection
          var elems = graphicsLayer.GetSelectedElements();
          //TODO process the selection...
        }
        else
        {
          //This is an unselect or clear selection
          //TODO process the unselect or clear select
        }
      }
    });
    Group Graphic Elements
    var graphicsLayer = MapView.Active.Map.GetLayersAsFlattenedList()
                        .OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();
    if (graphicsLayer == null)
      return;
    
    var elemsToGroup = graphicsLayer.GetSelectedElements();
    //Note: run within the QueuedTask
    //group  elements
    var groupElement = graphicsLayer.GroupElements(elemsToGroup);
    Un-Group Graphic Elements
    var selectedElements = graphicsLayer.GetSelectedElements().ToList(); ;
    if (selectedElements?.Any() == false)//must be at least 1.
      return;
    var elementsToUnGroup = new List<GroupElement>();
    //All selected elements should be grouped.
    if (selectedElements.Count() == selectedElements.OfType<GroupElement>().Count())
    {
      //Convert to a GroupElement list.
      elementsToUnGroup = selectedElements.ConvertAll(x => (GroupElement)x);
    }
    if (elementsToUnGroup.Count() == 0)
      return;
    //UnGroup
    graphicsLayer.UnGroupElements(elementsToUnGroup);
    Ordering: Send backward and Bring forward
    //On the QueuedTask
    //get the current selection set
    var sel_elems = graphicsLayer.GetSelectedElements();
    //can they be brought forward? This will also check that all elements have the same parent
    if (graphicsLayer.CanBringForward(sel_elems))
    {
      //bring forward
      graphicsLayer.BringForward(sel_elems);
      //bring to front (of parent)
      //graphicsLayer.BringToFront(sel_elems);
    }
    else if (graphicsLayer.CanSendBackward(sel_elems))
    {
      //send back
      graphicsLayer.SendBackward(sel_elems);
      //send to the back (of parent)
      //graphicsLayer.SendToBack(sel_elems);
    }
    Get Z-Order
    var selElementsZOrder = graphicsLayer.GetSelectedElements();
    //list out the z order
    foreach (var elem in selElementsZOrder)
      System.Diagnostics.Debug.WriteLine($"{elem.Name}: z-order {elem.ZOrder}");
    Move Graphic Elements
    //Each selected element will move to a set distance to the upper right.
    var selElements = graphicsLayer.GetSelectedElements();
    if (selElements.Count == 0) return;
    //Move the element up
    foreach (var selElement in selElements)
    {
      //Get the element's bounds
      var elementPoly = PolygonBuilderEx.CreatePolygon(selElement.GetBounds());
      //get the coordinates of the element bounding envelope.
      var pointsList = elementPoly.Copy2DCoordinatesToList();
      //Move the element's Anchor point to the upper right.
      selElement.SetAnchorPoint(pointsList[1]);
    }
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also