ArcGIS Pro 3.7 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 Shared Function GetSelectedElements( _
       ByVal graphicsLayer As GraphicsLayer _
    ) As IReadOnlyList(Of Element)
    public static IReadOnlyList<Element> GetSelectedElements( 
       GraphicsLayer graphicsLayer
    )

    Parameters

    graphicsLayer

    Return Value

    Remarks
    The map view displaying the graphics layer should be initialized.
    Example
    Remove Graphic elements
    {
      // Note: must be called on the QueuedTask
      {
        graphicsLayer.RemoveElements(graphicsLayer.GetSelectedElements());
      }
    }
    Un-Select Graphic elements
    {
      //unselect the first element in the currently selected elements
      var elem = graphicsLayer.GetSelectedElements().FirstOrDefault();
      // Note: must be called on the QueuedTask
      {
        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
    {
      // Note: must be called on the QueuedTask
      {
        var elemsToGroup = graphicsLayer.GetSelectedElements();
        //group  elements
        groupElement = graphicsLayer.GroupElements(elemsToGroup);
      }
    }
    Un-Group Graphic Elements
    {
      // Note: must be called on the QueuedTask
      {
        var selectedElements = graphicsLayer.GetSelectedElements().ToList(); ;
        if (selectedElements?.Count == 0)//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
    {
      // Note: must be called 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
    {
      // Note: must be called on the QueuedTask
      {
        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
    {
      // Note: must be called on the QueuedTask
      {
        //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 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also