Change color of the selection set on a layer

2459
2
Jump to solution
06-08-2016 03:16 PM
Highlighted
Occasional Contributor

I hope this is the right place to post this question.  I'm trying to change the selection color for a layer to red using the code below.  Although I am able to generate a selection, the color for the features ends up being the default color (cyan).

foreach (var x in MapView.Active.Map.Layers)

{

    if (x.Name == "Distribution Lines")

    {

        foreach (FeatureLayer fl in ((GroupLayer)x).Layers)

        {

            try

            {

                QueuedTask.Run(() =>

                {

                    CIMBasicFeatureLayer cbm = (CIMBasicFeatureLayer)fl.GetDefinition();

                    cbm.SelectionColor = CIMColor.CreateRGBColor(255, 0, 0);

                    fl.Select(queryFilter, SelectionCombinationMethod.New);

                                  

                });

            }

            catch (Exception ex)

            {

            }

        }

    }

}

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Contributor

I think you are missing the "SetDefinition" call on the layerdef. There is a snippet that shows what you want to do here:

Change the layer selection color

EDIT: sorry - looks like that snippet hasn't posted yet. This is it:

var featureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() => {

   // get the CIM definition of the layer

   var layerDef = featureLayer.GetDefinition() as ArcGIS.Core.CIM.CIMBasicFeatureLayer;

   // disable the default symbol

  layerDef.UseSelectionSymbol = false;

   // assign a new color

  layerDef.SelectionColor = ColorFactory.RedRGB;

   // apply the definition to the layer

  featureLayer.SetDefinition(layerDef);

   if (!featureLayer.IsVisible) featureLayer.SetVisibility(true);

   //Do a selection

  MapView.Active.SelectFeatures(MapView.Active.Extent);

});

View solution in original post

Reply
0 Kudos
2 Replies
Highlighted
Esri Contributor

I think you are missing the "SetDefinition" call on the layerdef. There is a snippet that shows what you want to do here:

Change the layer selection color

EDIT: sorry - looks like that snippet hasn't posted yet. This is it:

var featureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() => {

   // get the CIM definition of the layer

   var layerDef = featureLayer.GetDefinition() as ArcGIS.Core.CIM.CIMBasicFeatureLayer;

   // disable the default symbol

  layerDef.UseSelectionSymbol = false;

   // assign a new color

  layerDef.SelectionColor = ColorFactory.RedRGB;

   // apply the definition to the layer

  featureLayer.SetDefinition(layerDef);

   if (!featureLayer.IsVisible) featureLayer.SetVisibility(true);

   //Do a selection

  MapView.Active.SelectFeatures(MapView.Active.Extent);

});

View solution in original post

Reply
0 Kudos
Highlighted
by Esri Regular Contributor
Esri Regular Contributor

After you change the Selection color try to use 'SetDefinition' on your FeatureLayer as in (from API reference guide):

public void SetDefinition(

   CIMBaseLayer baseLayer

)

GetDefinition only returns a copy of ArcGIS Pro's internal settings (not a reference), SetDefinition will then post your changes back to ArcGIS Pro.
Also you can find some samples dealing with the CIM here: https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Map-Authoring/CIMExamples

Reply
0 Kudos