Pro SDK Create Legend by Layers

558
1
06-26-2018 08:12 AM
MatthewDriscoll
MVP Alum

Is there a way to add or subtract Layers to a Legend using the Pro SDK?  I see you can create a legend for a specific map frame, but I cannot figure out how to customize the legend once it is created.  

Tags (3)
0 Kudos
1 Reply
LesleyBross1
New Contributor III

This is not elegant, but it seems to work. You can use the LayoutElementFactory to create the legend. Then loop through all of the CIMLegendItem(s) in the LegendDefinition. Make an array out of the ones that you want to keep. You can identify them by name. Then set your new array as the CIMLegendItem(s) in the Definition and reset the Definition. This is definitely not as intuitive as it was in ArcObjects:

Legend legendElm = LayoutElementFactory.Instance.CreateLegend(layout, leg_env, mapFrame);
legendElm.SetName(Constants.MAPS_LEGEND);
legendElm.SetAnchor(Anchor.BottomLeftCorner);

CIMLegend cimLeg = legendElm.GetDefinition() as CIMLegend;
CIMLegendItem[] myLegendItems = new CIMLegendItem[1];
foreach (CIMLegendItem legItem in cimLeg.Items)
{
   if (legItem.Name.Equals(Constants.MAPS_ELEV_ZONE))

   {
      myLegendItems[0] = legItem;
      break;
   }
}
if (myLegendItems[0] != null)
{
   cimLeg.Items = myLegendItems;
   legendElm.SetDefinition(cimLeg);
}

0 Kudos