Select to view content in your preferred language

How can i set the layers showing in a Legend control?

2034
3
Jump to solution
02-04-2014 02:11 AM
IkerBerasaluce
Occasional Contributor
I tried setting a the LayerIDs property but it doesn't show anything since all the layers are hanging from a main one.
I also tried with the ShowOnlyVisibleLayers and this works pretty good, but when I check any layer it dissapears from the control since it only shows visible layers.
There is any way of selecting which layers and sublayers you want to show up in the legend?

Thank you.
0 Kudos
1 Solution

Accepted Solutions
ChrisBradberry
Deactivated User
I put this code in the MainPage.xaml.cs and it works for me.

    private void Legend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e)         {             // remove useless street map sublayers             if (e.LayerItem.Layer.ID == "BaseLayer")                 e.LayerItem.IsHidden = true;              if (e.LayerItem.Layer is GraphicsLayer)             {                 if (e.LayerItem.Layer is FeatureLayer)                 {                     if (e.LayerItem.Layer.ID == "PLSSFeatureLayer" || e.LayerItem.Layer.ID == "TownshipRange" || e.LayerItem.Layer.ID == "CurrentYearLightningFeatureLayer")                     {                         e.LayerItem.IsHidden = true;                     }                     else                     {                         e.LayerItem.IsHidden = false;                     }                 }                 else                 {                     e.LayerItem.IsHidden = true;                 }             }              Legend legend = sender as Legend;                  _theObservableCollection = MyLegend.LayerItems;              foreach (LayerItemViewModel livm in _theObservableCollection) // changed to the global             {                 livm.IsExpanded = false;             }             if (e.LayerItem.LayerItems != null)             {                 foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)                 {                     if (layerItemVM.IsExpanded)                         layerItemVM.IsExpanded = false;                     if (layerItemVM.LayerItems != null)                         foreach (var item in layerItemVM.LayerItems)                             item.IsExpanded = false;                     if (layerItemVM.LayerItems != null)                         foreach (LayerItemViewModel sublayerItemVM in layerItemVM.LayerItems)                             if (sublayerItemVM.IsExpanded)                                 sublayerItemVM.IsExpanded = false;                   }              }             else             {                 e.LayerItem.IsExpanded = false;             }          } 

View solution in original post

0 Kudos
3 Replies
ChrisBradberry
Deactivated User
I put this code in the MainPage.xaml.cs and it works for me.

    private void Legend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e)         {             // remove useless street map sublayers             if (e.LayerItem.Layer.ID == "BaseLayer")                 e.LayerItem.IsHidden = true;              if (e.LayerItem.Layer is GraphicsLayer)             {                 if (e.LayerItem.Layer is FeatureLayer)                 {                     if (e.LayerItem.Layer.ID == "PLSSFeatureLayer" || e.LayerItem.Layer.ID == "TownshipRange" || e.LayerItem.Layer.ID == "CurrentYearLightningFeatureLayer")                     {                         e.LayerItem.IsHidden = true;                     }                     else                     {                         e.LayerItem.IsHidden = false;                     }                 }                 else                 {                     e.LayerItem.IsHidden = true;                 }             }              Legend legend = sender as Legend;                  _theObservableCollection = MyLegend.LayerItems;              foreach (LayerItemViewModel livm in _theObservableCollection) // changed to the global             {                 livm.IsExpanded = false;             }             if (e.LayerItem.LayerItems != null)             {                 foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)                 {                     if (layerItemVM.IsExpanded)                         layerItemVM.IsExpanded = false;                     if (layerItemVM.LayerItems != null)                         foreach (var item in layerItemVM.LayerItems)                             item.IsExpanded = false;                     if (layerItemVM.LayerItems != null)                         foreach (LayerItemViewModel sublayerItemVM in layerItemVM.LayerItems)                             if (sublayerItemVM.IsExpanded)                                 sublayerItemVM.IsExpanded = false;                   }              }             else             {                 e.LayerItem.IsExpanded = false;             }          } 
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The solution proposed by Chris is correct and is even the only way to manage visibility of sublayers in the legend.

For the layers you don't want in the legend, you can also set the ShowLegend flag to false (either in XAML or by code).
0 Kudos
IkerBerasaluce
Occasional Contributor
Yes it does.
I finished adapting  Bradberry's code to my needs and it is working perfectly.

Thank you both for your help.
0 Kudos