Select to view content in your preferred language

Get the list of layers (in ArcGISDynamicMapServiceLayer) in the listbox at runtime.

1320
3
07-01-2010 12:41 PM
SanjayRajput
Deactivated User
I am trying to build a listbox of layers in the ArcGISDynamicMapServiceLayer. This listbox will be loaded initially with list of layers having checkbox beside it (to show and set the layer visibility). Is that possible first of all? Basically I building my own TOC. Below is the code snippet but on startup its throwing unknown exeception on this line : LayerVisibilityListBox.ItemsSource = visibleLayerList;

       <!-- Map-->
        <esri:Map x:Name="MyMap" Grid.Row="0" Grid.Column="1" >
            <esri:ArcGISDynamicMapServiceLayer ID="MyLayer" Initialized="DynamicLayer_Initialized" 
                Url="http://srajput/arcgis/rest/services/LCEC20/MapServer" />
        </esri:Map>

        <!-- Toggling Layer visibilty-->
        <Canvas x:Name="LayerListCanvas" HorizontalAlignment="Right" VerticalAlignment="Top" 
                Width="150" Height="180" Margin="0,10,10,0" Visibility="Visible" Grid.Row="0" Grid.Column="0">
            <Rectangle Canvas.Left="0" Canvas.Top="4" Width="150" Height="180"
                       RadiusX="10" RadiusY="10" Fill="#22000000" />
            <Rectangle Canvas.Left="0" Canvas.Top="0" Width="150" Height="180"
                       RadiusX="10" RadiusY="10" Fill="#775C90B2" Stroke="Gray" />
            <Rectangle Canvas.Left="10" Canvas.Top="17" Width="130" Height="165"
                       RadiusX="5" RadiusY="5" Fill="#FFFFFFFF" Stroke="DarkGray" />
            <TextBlock Text="Layer Visibility" Canvas.Left="15" Canvas.Top="2" Foreground="Black" />
            <ScrollViewer Grid.Row="2" Canvas.Left="10" Canvas.Top="15" Width="130" Height="165" 
                          HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
                <ListBox x:Name="LayerVisibilityListBox" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Margin="2" Name="{Binding LayerIndex}" Content="{Binding LayerName}" 
                                      Tag="{Binding ServiceName}" IsChecked="{Binding Visible}" 
                                      ClickMode="Press" Click="CheckBox_Click"   />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </ScrollViewer>
        </Canvas>


AND THE CODE BEHIND IS:

void DynamicLayer_Initialized(object sender, EventArgs args)
        {
            ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer =
                sender as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
            if (dynamicServiceLayer.VisibleLayers == null)
                dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer);
            UpdateLayerList(dynamicServiceLayer);
        }

        private int[] GetDefaultVisibleLayers(ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicService)
        {
            List<int> visibleLayerIDList = new List<int>();

            ESRI.ArcGIS.Client.LayerInfo[] layerInfoArray = dynamicService.Layers;

            for (int index = 0; index < layerInfoArray.Length; index++)
            {
                if (layerInfoArray[index].DefaultVisibility)
                    visibleLayerIDList.Add(index);
            }

            return visibleLayerIDList.ToArray();
        }


        private void UpdateLayerList(ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer)
        {
            int[] visibleLayerIDs = dynamicServiceLayer.VisibleLayers;

            if (visibleLayerIDs == null)
                visibleLayerIDs = GetDefaultVisibleLayers(dynamicServiceLayer);

            List<LayerListData> visibleLayerList = new List<LayerListData>();

            ESRI.ArcGIS.Client.LayerInfo[] layerInfoArray = dynamicServiceLayer.Layers;

            for (int index = 0; index < layerInfoArray.Length; index++)
            {
                visibleLayerList.Add(new LayerListData()
                {
                    Visible = visibleLayerIDs.Contains(index),
                    ServiceName = dynamicServiceLayer.ID,
                    LayerName = layerInfoArray[index].Name,
                    LayerIndex = index
                });
            }
            
            LayerVisibilityListBox.ItemsSource = visibleLayerList;        }


        void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox tickedCheckBox = sender as CheckBox;

            string serviceName = tickedCheckBox.Tag.ToString();
            bool visible = (bool)tickedCheckBox.IsChecked;

            int layerIndex = Int32.Parse(tickedCheckBox.Name);

            ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = MyMap.Layers[serviceName] as
                ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;

            List<int> visibleLayerList =
                dynamicServiceLayer.VisibleLayers != null
                ? dynamicServiceLayer.VisibleLayers.ToList() : new List<int>();

            if (visible)
            {
                if (!visibleLayerList.Contains(layerIndex))
                    visibleLayerList.Add(layerIndex);
            }
            else
            {
                if (visibleLayerList.Contains(layerIndex))
                    visibleLayerList.Remove(layerIndex);
            }

            dynamicServiceLayer.VisibleLayers = visibleLayerList.ToArray();

            if (visibleLayerList.Count < 1)
            {
                MyMap.Extent = MyMap.Extent;
            }
        }
 public class LayerListData
    {
        public bool Visible { get; set; }
        public string ServiceName { get; set; }
        public string LayerName { get; set; }
        public int LayerIndex { get; set; }
    }
0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
Is that possible first of all?

Sure : see sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SubLayerList

 
<CheckBox Margin="2" Name="{Binding LayerIndex}" Content="{Binding LayerName}" 
        Tag="{Binding ServiceName}" IsChecked="{Binding Visible}" 
         ClickMode="Press" Click="CheckBox_Click"   />
 

From SL4, binding to the name property is no more working. The sample has been adapted in order not to use this binding.
0 Kudos
KeithGanzenmuller
Frequent Contributor
I am a little confused by the reply, can someone straighten me out.

Do the sub layers of a dynamic map service layer have a visible property?
The only one I have been able to use is the DefaultVisibilty which tells you if the sub-layer is visible when the layer is initialized.
Is that right?

Is there another way to turn sub-layers on and off other than to use the VisibleLayers property of the parent layer?

Keith
0 Kudos
KirkKuykendall
Deactivated User
One of the nice benefits of the legend is that LayerItemViewModel.IsVisible works for this.  Sometimes it gets out of synch with the map though (when user zooms in really quick), so I ended up exposing a button that calls Legend.Refresh.
0 Kudos