Select to view content in your preferred language

Removing sublayers from the legend

1195
2
Jump to solution
06-18-2012 08:06 AM
DanielSchatt
Regular Contributor
hi!  I'm trying to get rid of sublayers from the ESRI services in my legend using the exact same code that's in the "Legend with Templates" sample, but it isn't working.  The services with one sublayer are having that sublayer removed, but there's another service (ESRI's World_Imagery service) with four sublayers and it's only getting rid of one of them (World Imagery).  It appears that the other three sublayers in that service aren't in the e.LayerItem.LayerItems collection.  Code is below.  Thanks for any help! 

Dan

        private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)         {             LayerItemViewModel removeLayerItemVM = null;              if (e.LayerItem.LayerItems != null)             {                 foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)                 {                     if (layerItemVM.IsExpanded)                         layerItemVM.IsExpanded = false;                           if ((layerItemVM.Label == "USA Topo Maps") || (layerItemVM.Label == "World Street Map") || (layerItemVM.Label == "World Imagery") || (layerItemVM.Label == "Low Resolution 15m Imagery") || (layerItemVM.Label == "High Resolution 60cm Imagery") || (layerItemVM.Label == "High Resolution 30cm Imagery"))                         removeLayerItemVM = layerItemVM;              if (removeLayerItemVM != null)                         e.LayerItem.LayerItems.Remove(removeLayerItemVM);                   }             }             else             {                 e.LayerItem.IsExpanded = false;             }         }
0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor
Legend.Refreshed event is fired by layer: e.LayerItem is the main layer having the legend refreshed (i.e the ArcGISTiledMapServiceLayer) and e.LayerItem.LayerItems is the collection of sublayers.
The confusion is that often one sublayer has the same name as the layer.

For example in case of the 'World Imagery' layer the sublayers are World Imagery, Low Resolution 15m Imagery, Low Resolution 15m Imagery, High Resolution 60cm Imagery and High Resolution 30cm Imagery. So your code is removing the sublayer 'World Imagery' and keeps the 3 others.

If you want to remove all sublayers of any ArcGISTiledMapServiceLayer, you can use code such as :

private void Legend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e) {   if (e.LayerItem.Layer is ArcGISTiledMapServiceLayer)     e.LayerItem.LayerItems = null; }

View solution in original post

0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor
Legend.Refreshed event is fired by layer: e.LayerItem is the main layer having the legend refreshed (i.e the ArcGISTiledMapServiceLayer) and e.LayerItem.LayerItems is the collection of sublayers.
The confusion is that often one sublayer has the same name as the layer.

For example in case of the 'World Imagery' layer the sublayers are World Imagery, Low Resolution 15m Imagery, Low Resolution 15m Imagery, High Resolution 60cm Imagery and High Resolution 30cm Imagery. So your code is removing the sublayer 'World Imagery' and keeps the 3 others.

If you want to remove all sublayers of any ArcGISTiledMapServiceLayer, you can use code such as :

private void Legend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e) {   if (e.LayerItem.Layer is ArcGISTiledMapServiceLayer)     e.LayerItem.LayerItems = null; }
0 Kudos
DanielSchatt
Regular Contributor
Works great! Thanks much Dominique
0 Kudos