ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / MapView Class / GetSelectedLayers Method
Example

In This Topic
    GetSelectedLayers Method
    In This Topic
    Returns the collection of layers selected in the TOC.
    Syntax
    public IReadOnlyList<Layer> GetSelectedLayers()
    Public Function GetSelectedLayers() As IReadOnlyList(Of Layer)

    Return Value

    The collection of layers selected in the TOC.
    Example
    Group and ungroup layers
    {
      // group a set of layers selected in the TOC
      var selectedLayers = mapView.GetSelectedLayers();
      if (map.CanGroupMapMembers(selectedLayers))
      {
        map.GroupMapMembers(selectedLayers, "Group1");
      }
    
      // ungroup a groupLayer
      if (map.CanUngroupLayer(groupLayer))
      {
        map.UngroupLayer(groupLayer);
      }
    }
    Create a Lyrx file from mulltiple mapMembers
    {
      // get the layers selected in the TOC
      var selectedLayers = MapView.Active.GetSelectedLayers();
      // get the standalonetables elected in the TOC
      var selectedTables = MapView.Active.GetSelectedStandaloneTables();
    
      var selectedMMs = new List<MapMember>(selectedLayers);
      selectedMMs.AddRange(selectedTables);
    
      // create the LayerDocument
      LayerDocument layerDocument = new LayerDocument(selectedMMs);
      await QueuedTask.Run(() => layerDocument.Save(@"c:\Data\MyLayerDocument.lyrx"));
    }
    Find a MapView by its Caption
    {
      string mapPaneCaption = "USNationalParks";
      IMapPane mapViewPane = FrameworkApplication.Panes.OfType<IMapPane>().FirstOrDefault((p) => p.Caption == mapPaneCaption);
      mapView = null;
      if (mapViewPane != null)
      {
        // activate the MapPane
        (mapViewPane as Pane).Activate();
    
        if (mapView != null)
        {
          // get the layers selected in the map's TOC
          var selectedLayers = mapView.GetSelectedLayers();
        }
      }
    }
    Zoom To Selected Layers
    {
      //Zoom to the selected layers in the TOC
      var selectedLayers = mapView.GetSelectedLayers();
      mapView.ZoomToAsync(selectedLayers);
    }
    Pan To Selected Layers Asynchronous
    {
      //Pan to the selected layers in the TOC
      var selectedLayers = mapView.GetSelectedLayers();
      mapView.PanToAsync(selectedLayers);
    }
    Get a Voxel Layer from the TOC
    {
      //Get selected layer if a voxel layer is selected
      voxelLayer = MapView.Active.GetSelectedLayers().OfType<VoxelLayer>().FirstOrDefault();
      if (voxelLayer == null)
      {
        //just get the first voxel layer in the TOC
        voxelLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<VoxelLayer>().FirstOrDefault();
        if (voxelLayer == null)
        {
          return;
        }
      }
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also