I'm working on an Add-In and need to be able to use layers that are currently added in the Map's content pane. I would like to see if there is a ProSnippet that lets me directly add this component. The component that I would like to use looks like this and I see it all around ArcGIS Pro. It also lets you use geometry that you create on-the-fly.
The closest thing I could find was using a ComboBox in combination with Map class's GetLayersAsFlattenedList method. But this still leaves a lot of functionality missing from the component I would've like to use.
Solved! Go to Solution.
There's nothing "built-in" but we have lots of examples of combos in panes, dockpanes, etc. that list the names of layers from the map, etc. in the samples.
Here's one u can look at that should help get u started: https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Map-Authoring/LayersPane - Specifically, look at the layers pane combobox implementation in https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/master/Map-Authoring/LayersPane/UI/Lay.... It is bound to a property "AllMapLayers" in the view model. It is just showing feature layers but u can change that to be whichever layer types - as you need.
There are probably others.
There's nothing "built-in" but we have lots of examples of combos in panes, dockpanes, etc. that list the names of layers from the map, etc. in the samples.
Here's one u can look at that should help get u started: https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Map-Authoring/LayersPane - Specifically, look at the layers pane combobox implementation in https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/master/Map-Authoring/LayersPane/UI/Lay.... It is bound to a property "AllMapLayers" in the view model. It is just showing feature layers but u can change that to be whichever layer types - as you need.
There are probably others.
Hello @CharlesMacleod ,
Thank you for this solution. This answers my original question. Playing with this further, however, the LayersPane Combobox does not update when a new layer is added. The way AllMapLayers is bound to the ComboBox ItemsSource property, does it not allow automatic updating? I also used the LayersAddedEvent, by running the following when triggered.
_allMapLayers = activeView.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().ToList();
While this does actually update the _allMapLayers variable (I was able to verify using a Dialogs.MessageBox which displayed total layer count in the map), it doesn't update the combobox on-the-fly. The LayersPane sample you provided doesn't have this functionality either.
What's the best remedy here?
Thanks for your time!
Yes, things can get complicated quickly. If you need your combo to remain current "auto-magically" as the map TOC changes you will need to handle all events associated with changes to the TOC.
Start with:
ArcGIS.Desktop.Mapping.Events.LayersAddedEvent
ArcGIS.Desktop.Mapping.Events.LayersRemovedEvent
In each of these add or remove the relevant layer(s) to your list
If the user can switch (active) maps, then you will need to handle this one also:
ArcGIS.Desktop.Mapping.Events.ActiveMapViewChangedEvent.
It will fire twice when a map pane switches - once when the (old) view is deactivated and once when the new view is activated. If a user selects a Layout/Chart/or other non-map pane then it fires just once (for the old view being deactivated).
For "automatic" updating, as u call it, u would need an ObservableCollection and not a (generic) List. However, switching to an ObservableCollection will require u to handle the multi-threaded access (from the UI and, potentially, from the QueuedTask) which adds another level of complexity. (fyi This was covered at Dev Summit 2022 here: https://esri.github.io/arcgis-pro-sdk/techsessions/2022/PalmSprings/ImprovingYourDockpaneandProWindo...