Remove legend items from layout when no feature for this layer on the map extent.

438
2
Jump to solution
08-11-2021 06:33 AM
FredB
by
New Contributor II

Hello,

In ArcGIS Pro, there is an option in the legendElement from layout to display only legend items when at least one feature is displayed on the map extent (Only show features visible in the map extent).

Is there such an option in the SDK or should I loop through the MapFrame's layer, make a check and then remove the element from the legend.

Thanks.

0 Kudos
1 Solution

Accepted Solutions
CharlesMacleod
Esri Regular Contributor

I am assuming u are referring to this setting....

auto_visibility.png

which I am not personally familiar with but...if that _is_ the property u are interested in, with a bit of trial and error, it turns out it is this guy here: CIMLegendItem.AutoVisibility

This snippet toggles that AutoVisibility for all legend items. I noticed that the legend item property UI did not automatically refresh to reflect the "toggled" value but that is a bug (and I will relay that to the layout team). To update the UI I had to click on the legend and then back on the legend item.

internal class ToggleAutoVisibility : Button
	{
		protected override void OnClick()
		{
			var lyt = LayoutView.Active?.Layout;
			if (lyt == null)
				return;
			QueuedTask.Run(() =>
			{
				var def = lyt.GetDefinition();
				var legend = def.Elements.OfType<CIMLegend>().FirstOrDefault();
				if (legend != null)
				{
					//toggle auto visibility of all the legend items
					foreach(var leg_item in legend.Items)
					{
						leg_item.AutoVisibility = !leg_item.AutoVisibility;
					}
					//commit the change
					lyt.SetDefinition(def);
				}
			});
		}
	}

 

View solution in original post

2 Replies
CharlesMacleod
Esri Regular Contributor

I am assuming u are referring to this setting....

auto_visibility.png

which I am not personally familiar with but...if that _is_ the property u are interested in, with a bit of trial and error, it turns out it is this guy here: CIMLegendItem.AutoVisibility

This snippet toggles that AutoVisibility for all legend items. I noticed that the legend item property UI did not automatically refresh to reflect the "toggled" value but that is a bug (and I will relay that to the layout team). To update the UI I had to click on the legend and then back on the legend item.

internal class ToggleAutoVisibility : Button
	{
		protected override void OnClick()
		{
			var lyt = LayoutView.Active?.Layout;
			if (lyt == null)
				return;
			QueuedTask.Run(() =>
			{
				var def = lyt.GetDefinition();
				var legend = def.Elements.OfType<CIMLegend>().FirstOrDefault();
				if (legend != null)
				{
					//toggle auto visibility of all the legend items
					foreach(var leg_item in legend.Items)
					{
						leg_item.AutoVisibility = !leg_item.AutoVisibility;
					}
					//commit the change
					lyt.SetDefinition(def);
				}
			});
		}
	}

 

FredB
by
New Contributor II

That was it. Thanks !

0 Kudos