How to remove specific layer from the Legend control?

939
6
Jump to solution
07-16-2012 03:21 AM
SanajyJadhav
Occasional Contributor II
Hello,

I am following this sample to create legend.

My requirement is to remove the layer from the legend when it is turned off due to scale dependant visibility and add it back to the legend when it is turned on.This layer could be feature layer or any sub layer from the map service.I guess, I have to call Legend::Refresh in the Map's extent changed event.

However,I am not being able to figure-out how to do this.To be frank,I am having hard time to understand the LayerItemViewModel class.

So can anybody help me on this issue?I would appreciate it.

I have attached screen shot of my Legend control with map.

Thanks.
0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor
Thanks for the replies.

I tried changing that property but unfortunately it does not help me.If the layer is removed from the Legend, it was not getting added back when it was again made visible.

If you are using the option 'ShowOnlyVisibleLayers' and that a layer is removed from the legend because the layer is no more visible (for example because you zoom out of the scale range), the layer should automatically show up again in the legend when the layer is again made visible.
Could you give a sample to reproduce this issue?


Also, I noticed that the LayerItemViewModel class returns strange values for certain properties.e.g. If the layer is out of visible scale range, it should return false for the property IsInScaleRange.But every time, it returns true.Same is the case for IsVisible and IsMapLayer.


Not sure about the strange values you noticed but these points might explain your results:

  • IsInScaleRange is always true if there is no minimum scale or maximum scale defined for the layer or the sublayer.

  • IsMapLayer is always true for the main layers (i.e the layers declared in the map). It's false for the sublayers of an ArcGISDynamicMapServiceLayer or ArcGISTiledMapServiceLayer.

  • IsVisible should be false if your layer or sublayer is not visible (if the layer visibility has been set to false or if IsInScaleRange is false)

You will find more explanations about these properties in the documentation.

View solution in original post

0 Kudos
6 Replies
JaredWhite
New Contributor III
Hello,

I am following this sample to create legend.

My requirement is to remove the layer from the legend when it is turned off due to scale dependant visibility and add it back to the legend when it is turned on.This layer could be feature layer or any sub layer from the map service.I guess, I have to call Legend::Refresh in the Map's extent changed event.

However,I am not being able to figure-out how to do this.To be frank,I am having hard time to understand the LayerItemViewModel class.

So can anybody help me on this issue?I would appreciate it.

I have attached screen shot of my Legend control with map.

Thanks.


It's easier than you'd think. Just change ShowOnlyVisibleLayers from "False" to "True" from the sample.

  <esri:Legend Map="{Binding ElementName=MyMap}"  
                         LayerIDs="Points of Interest, United States"
                         LayerItemsMode="Tree" 
                         ShowOnlyVisibleLayers="True"
                         Refreshed="Legend_Refreshed">
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Jared is right. You can just set 'ShowOnlyVisibleLayers' to true.

Note however that the user will no more be able to turn on a layer using the legend since the non visible layers are not displayed in the legend.
0 Kudos
SanajyJadhav
Occasional Contributor II
Thanks for the replies.

I tried changing that property but unfortunately it does not help me.If the layer is removed from the Legend, it was not getting added back when it was again made visible.

Also, I noticed that the LayerItemViewModel class returns strange values for certain properties.e.g. If the layer is out of visible scale range, it should return false for the property IsInScaleRange.But every time, it returns true.Same is the case for IsVisible and IsMapLayer.

I am really confused about this class.Can anybody shed some light on usage of this class to solve my issue if possible at all?

Thanks.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Thanks for the replies.

I tried changing that property but unfortunately it does not help me.If the layer is removed from the Legend, it was not getting added back when it was again made visible.

If you are using the option 'ShowOnlyVisibleLayers' and that a layer is removed from the legend because the layer is no more visible (for example because you zoom out of the scale range), the layer should automatically show up again in the legend when the layer is again made visible.
Could you give a sample to reproduce this issue?


Also, I noticed that the LayerItemViewModel class returns strange values for certain properties.e.g. If the layer is out of visible scale range, it should return false for the property IsInScaleRange.But every time, it returns true.Same is the case for IsVisible and IsMapLayer.


Not sure about the strange values you noticed but these points might explain your results:

  • IsInScaleRange is always true if there is no minimum scale or maximum scale defined for the layer or the sublayer.

  • IsMapLayer is always true for the main layers (i.e the layers declared in the map). It's false for the sublayers of an ArcGISDynamicMapServiceLayer or ArcGISTiledMapServiceLayer.

  • IsVisible should be false if your layer or sublayer is not visible (if the layer visibility has been set to false or if IsInScaleRange is false)

You will find more explanations about these properties in the documentation.
0 Kudos
SanajyJadhav
Occasional Contributor II
You are right Dominique.

I tested on this online service and noticed following things.

If 'ShowOnlyVisibleLayers' is set to true then,

1. If the layer is turned off due to scale dependency, it would get removed from Legend.And if it is again turned due to current map scale on, then it is getting added back to the Legend.
2. If the layer is not scale range, then 'IsInScaleRange' property returns the right value accordingly.
3. Remaining properties also behave consistently.

God knows why its acting weird on my service.

But still, I can't use 'ShowOnlyVisibleLayers' property since I have to provide Check box before layer name.If user turns off the layer using this check box,then layer would not get re-added to Legend.Am i right?

Thanks.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
But still, I can't use 'ShowOnlyVisibleLayers' property since I have to provide Check box before layer name.If user turns off the layer using this check box,then layer would not get re-added to Legend.Am i right?


Sure. If the off layers are not displayed in the legend, the user won't be able to turn them on from the legend.
0 Kudos