Select to view content in your preferred language

LayerItemViewModel.LayerItems returning null for dynamic layer

772
2
07-01-2011 02:09 PM
MarcusHarner
Regular Contributor
I used the ???Legend with Templates??? example to create a legend with checkboxes for layers within a map layer.  It works wonderfully.  I???ve been trying to have create combo boxes populated the visible layers (which varies with scale and which layers a checked in the legend).  After about a week of testing various ideas I think using the LayerItemViewModel and LayerItems seems to be almost working to provide a list of the visible layers. 

It works great the first time I load the page, but when I turn on/off a layer in the legend the LayersItems returns null.   

I effectively run this code when a layer is checked on/off

Layer srvcLayer = null;
ObservableCollection<LayerItemViewModel> srvcLyrViewModels = DisplayLegend.LayerItems; 
// DisplayLegend it the name of the legend
foreach (LayerItemViewModel srvcLyrViewModel in srvcLyrViewModels)
// srvcLyrViewModel is the service layer in the map
{
    if (srvcLyrViewModel.IsVisible)
    {
        srvcLayer = srvcLyrViewModel.Layer;
        if (srvcLayer is ArcGISDynamicMapServiceLayer)
        {
            ObservableCollection<LayerItemViewModel> lyrViewModels = srvcLyrViewModel.LayerItems;
            if (lyrViewModels != null && lyrViewModels.Count > 0)
            {
                foreach (LayerItemViewModel lyrViewModel in lyrViewModels)
                // lyrViewModel is the layer in the service
                {
                    if (lyrViewModel.IsVisible)
                    {
                        layerKey = srvcLyrViewModel.Label + " | " + lyrViewModel.Label;
                        MessageBox.Show(layerKey);
                    }
                }
            }
        }
    }
}

In the example, the lyrViewModels returns a null, even though there are layers in the service and in the legend.

Does anyone know if quirky issues with LegendItemViewModels or if I am totally not using them how they are meant to be used? 

Thanks in advance for any advice.

-Marcus Harner
0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor
It works great the first time I load the page, but when I turn on/off a layer in the legend the LayersItems returns null.


Due to this test:
 
if (srvcLyrViewModel.IsVisible)
{
 


you are looking in the visible layers only. So you won't find the item once the layer is no more visible.

That being said you don't need the legend to know whether a sublayer is visible or not. You can use GetSublayerVisibility as well http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.ArcGISDyna....
0 Kudos
MarcusHarner
Regular Contributor
Thanks Dominique!,

Yep, I am looking for just a list of the visible layers and sublayers, hence the
'if (srvcLyrViewModel.IsVisible)'
statement.

I tried the ArcGISDynamicMapServiceLayer.GetSublayerVisibility and it works fantastic except it does not seem to honor sublayers that are enabled but not visible on the map because they are not within the scale range of the map.

I was trying the 'LayerItemViewModel' approach because I saw in the �??Legend with Templates�?? ( http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LegendWithTemplates ) example the sublayers are enabled (via a binding in the xaml) to the IsInScaleRange property, which I found in the LayerItemViewModel class. 

In the API reference I found the LayerItemViewModel.IsVisible property ( http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client.Toolkit~ESRI.ArcGIS.Client.To... ) handles both the visibility and the scale range.

A puzzle indeed.
-Marcus
0 Kudos