Select to view content in your preferred language

Legend control expanding till layers symbology level + arcgis api for silverlight 3.0

2202
3
06-03-2013 10:10 PM
PhanindraKuchipudi
Emerging Contributor
Legend control with tree mode when expanding showing layer symbology also. My map service has combination of feature layers, group layers and raster layers. When I click on the Map service layer in legend with tree mode to see the layers available, it is expanding each and every layer item till its symbology level and making legend control lengthy. I don't want layer items to expand till their symbology level. I am using arcgis api for silverlight 3.0 interactive sample code for legend control with tree mode in my project. Can any one help me to get my job done?
0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
By default the legend tree nodes are expanded.
You can change this behavior either by restyling the legend control, or, likely easier, by code on the event Legend.Refreshed:

private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
{
    // Collapse all map layers in the legend.
    e.LayerItem.IsExpanded = false;

    // Collapse all sublayers
    if (e.LayerItem.LayerItems != null)
        foreach (var sublayerItem in e.LayerItem.LayerItems)
            sublayerItem.IsExpanded = false;
}
0 Kudos
PhanindraKuchipudi
Emerging Contributor
Thanks dbroux
0 Kudos
BrandonIves
Emerging Contributor
By default the legend tree nodes are expanded. 
You can change this behavior either by restyling the legend control, or, likely easier, by code on the event Legend.Refreshed: 

private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
{
    // Collapse all map layers in the legend.
    e.LayerItem.IsExpanded = false;

    // Collapse all sublayers
    if (e.LayerItem.LayerItems != null)
        foreach (var sublayerItem in e.LayerItem.LayerItems)
            sublayerItem.IsExpanded = false;
}



This worked for me when I wanted to collapse all layers and sublayers:


private void Legend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e)
{
// The purpose of the code in this function is to cause all of the leaves in the Legend to be collapsed
// to the highest level (i.e. the Layer level) when the application starts up. It is by setting the
// LayerItemViewModel.IsExpandable = False for each Layer/sub-Layer that allows this behavior to occur.
// Had this code-behind logic not been used all of the leaves would have been expanded to their lowest level
// in the Legend Control (this is the default behavior).

// Get the Legend.
ESRI.ArcGIS.Client.Toolkit.Legend theLegend = (ESRI.ArcGIS.Client.Toolkit.Legend)sender;

// Get the LayerItems of the Legend Control.
System.Collections.ObjectModel.ObservableCollection<ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel> theObservableCollection = theLegend.LayerItems;

// Loop through the ObservableCollection<LayerItemViewModel> objects for each Layer.
foreach (ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel theLayerItemViewModel in theObservableCollection)
{
// Close the leaves in the Legend Control for the Layers
theLayerItemViewModel.IsExpanded = false;

}
//close the leaves in the Legend Control for the sub layers

if (e.LayerItem.LayerItems != null)
foreach (var sublayerItem in e.LayerItem.LayerItems)
sublayerItem.IsExpanded = false;


}
0 Kudos