Controlling Layers in Legend Control

5289
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
karenvolarich
New Contributor III
I'm going to bounce one more question out there for you...

I have added the ability to dynamically add an ArcGISDynamicLayer Map Service to the map and to the TOC/Legend.  But, it places it at the bottom of the legend, and I would like to place it at the top.  There doesn't seem to be an "orderby" type method.  I was looking at LegendUpdated but am not seeing anything that might assist in the ordering of the dynamically added data.
0 Kudos
DominiqueBroux
Esri Frequent Contributor

I have added the ability to dynamically add an ArcGISDynamicLayer Map Service to the map and to the TOC/Legend. But, it places it at the bottom of the legend, and I would like to place it at the top.

By default, the entries in the legend are in the same order than the layers in the map. So, if you add a layer at the bottom of the map, it will be at the bottom of the legend.

If you want to change this behavior, the easier way is to set the LayerIds property. The legend will be ordered as the LayerIds.

For some scenarios, it might also be useful to reorder dynamically by code on the event Legend.Refreshed.
A code like this one should do the job:
private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
{
  Legend legend = sender as Legend;
 if (e.LayerItem.Layer.ID == "MyTopLayerID")
  {
    legend.LayerItems.Remove(e.LayerItem); // Remove legend entry from its current location
    legend.LayerItems.Insert(0, e.LayerItem); //Add it at the top
  } 
}
0 Kudos
BrandonIves
New Contributor

Hey Dominique,

I can successfully remove a legend entry with your example above. How would I go about re-adding an item after removing it? Once I remove the LayerItem from the Legend l no longer have access to that layer to use the insert method.

Ex:

if(HPGasLegend == false){

    if (layerItemVM.Layer.ID == "HP_Gas_Feeder_Layer")

    {

           Legend.LayerItems.Remove(e.LayerItem);

     }

}

else   

{

     // here is where I would like to reinsert the layer above that I removed based on a checkbox checked/unchecked event

     // when I hover over the legend object and look through the collection I no longer see my layer that with the ID of "HP_Gas_Feeder_Layer"

     // in the collection

}

Thanks for any help!

0 Kudos
DominiqueBroux
Esri Frequent Contributor

The easiest way to get a layer out of the legend is to set the layer.ShowLegend property to false.

So by binding this layer property to your checkbox, you should directly get the expected behavior.

Else, if for any reason you'd rather using the Legend_Refreshed Handler, a couple of possible options:

  - call Refresh on the Legend object (the drawback is that all legend layers will be refreshed)

  - keep the legend item in a private field when you removed it, so you can readd it when needed.

BrandonIves
New Contributor

I was attempting to keep a copy of the legend item and readd it but it seems like it will be overly complicated for multiple layers.

Could you give an example of the binding the layer property to the checkbox? 

Thanks!

0 Kudos
BrandonIves
New Contributor

Nevermind Dominique,

I got it to work with this code. Thank  you for your help in pointing me in the correct direction.

<CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="331,261,0,0" VerticalAlignment="Top"

DataContext="{Binding Layers, ElementName=MyMap}"

IsChecked="{Binding [GasLayer].ShowLegend, Mode=TwoWay}"  />

0 Kudos