Controlling Layers in Legend Control

3852
15
01-04-2011 04:02 AM
PaulHuppé
Occasional Contributor
Hi,

I am testing the new Legend Control in the 2.1 API.  Is there a way to control what is shown or not shown in the Table OF Contents?  In particular, I do not want to show the Graphics Layer used to display results.  There does not seem to be any options directly on the <esri:GraphicsLayer> or the <esri:Legend> tags.  What would be nice is to have a ShowInLegend="false" attribute on the <esri:GraphicsLayer> tag.  Is there an easy way to remove the layer from the legend?

Thanks,
Paul
0 Kudos
15 Replies
ErikEngstrom
Occasional Contributor II
Set the LayerIDs property of the legend control.

So for instance, you only want to display your baselayer in the legend:
<esri:Map>
<esri:ArcGISTiledMapServiceLayer ID="baseLayer" Url="..." />
</esri:Map>

in legend:
<esri:legend Map="{Binding ElementName=MyMap}"
LayerItemsMode="Tree"
LayerIDs="baseLayer"
ShowOnlyVisibleLayers="True" />

To add layers to your legend, just separate them by a comma.
0 Kudos
PaulHuppé
Occasional Contributor
Hi,

That might work, but if I have lots of layers, which I do, this list of IDs can become extremely long!!!  Is there a limit to the length this string can be?  I hope there is a better way than this because it may not be manageable for us.

Paul
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Additionaly to the LayerIds property mentionned by Erike, you can dynamically remove legend items in the legend refreshed event.

For example if you don't want to see any GraphicsLayer, you can add this code:
private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
{
if (e.LayerItem.Layer is ESRI.ArcGIS.Client.GraphicsLayer)
    MyLegend.LayerItems.Remove(e.LayerItem);
}
You can obviously extend this sample by using another criteria than the layer type : for example a custom attached property.
0 Kudos
PaulHuppé
Occasional Contributor
Hi,
I tried using the Remove in the Legend_Refreshed method as you suggested, and that does the trick.
Thanks,
Paul
0 Kudos
karenvolarich
New Contributor III
Is there a way to remove the Bing basemap from the Legend?  I tried using the same code as the GraphicsLayer, but it just doesn't see it.

if (e.LayerItem.Layer is ESRI.ArcGIS.Client.Bing.TileLayer)
    MyLegend.LayerItems.Remove(e.LayerItem);

I also tried:

if (layerItemVM.Layer.ID == "BingLayer")
          removeLayerItemVM = layerItemVM;             

Any ideas?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Is there a way to remove the Bing basemap from the Legend? I tried using the same code as the GraphicsLayer, but it just doesn't see it.


You have to hookup the handler to the Refreshed event:

<esri:Legend x:Name="MyLegend" Map="{Binding ElementName=MyMap}" Height="400" Width="400" 
Refreshed="Legend_Refreshed" >


Then both versions of your code should work.
0 Kudos
karenvolarich
New Contributor III
I already have that in my xaml:

<esri:Legend Map="{Binding ElementName=Map}"
                         Name="MyLegend"
                         LayerItemsMode="Tree"
                         ShowOnlyVisibleLayers="False"
                         Refreshed="Legend_Refreshed">

Any other ideas?

Thanks,
Karen
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You are right there is an issue.The event is not fired with Bing layers (because this kind of layer doesn't return any legend).This is a bug that we'll try to fix for next version.For the moment, the workaround might be to remove the bing layers from the legend whatever the layer firing the event:
private void Legend_Refreshed(object sender, Client.Toolkit.Legend.RefreshedEventArgs e)
{
foreach (var bingLayerItem in MyLegend.LayerItems.Where(item => item.Layer is TileLayer).ToList())
   MyLegend.LayerItems.Remove(bingLayerItem);
}


Not very elegant, but....
0 Kudos
karenvolarich
New Contributor III
That works!  Thanks.
0 Kudos