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