ArcGIS API for Silverlight - Library Reference
GetLayerVisibility Method
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client Namespace > ArcGISDynamicMapServiceLayer Class : GetLayerVisibility Method

layerID
The sub-layer ID.
Gets the sub-layer visibility.

Syntax

Visual Basic (Declaration) 
Public Function GetLayerVisibility( _
   ByVal layerID As Integer _
) As Boolean
C# 
public bool GetLayerVisibility( 
   int layerID
)

Remarks

The GetLayerVisibility Method returns a boolean for a specific sub-layer ID of an ArcGISDynamicMapServiceLayer telling whether it is visible (true) or not (false) in the web service. Do not use the ArcGISDynamicMapServiceLayer.VisibleLayers Property to try and determine which sub-layers are visible for the web service. The VisibleLayers Property will always return null/Nothing for the web service. Once the VisibleLayers Property has been set you can then you can use the get/Read operations of that Property to obtain valid results.

For most ArcGISDynamicMapServiceLayer Properties that have both get/set (C#) or Read/Write (VB.NET) Properties, accessing the get/Read for a particular Property will return null/Nothing from ArcGIS Server. The exceptions are DisableClientCaching which returns false and ImageFormat which returns ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer.RestImageFormat.PNG32; these are the default values. For all other get/set (C#) or Read/Write (VB.NET) Property values, in order to obtain get/Read information the application developer must first set/Write the values. Use the various ArcGISDynamicMapServiceLayer Methods to serve as a starting point to obtain ArcGIS Server metadata information for the set/Write Properties.

Another option for determining if the sub-layer of an ArcGISDynamicMapServiceLayer is visible or not is to enter the Url for the sub-layer in the address bar of an internet browser and look for the Default Visibility section in the ArcGIS REST Services Directory. Remember the sub-layer has the same Url as the main ArcGISDynamicMapServiceLayer but with a forward slash and the sub-layer ID value appended to the Url (ex: http://server.arcgisonline.com/ArcGIS/rest/services/Reference/ESRI_BoundariesPlaces_World_2D/MapServer/0). See the following screen shot:

Determining the sub-layer visibility using the ArcGIS REST Services Directory.

Parameters

layerID
The sub-layer ID.

Return Value

The sub-layer visibility

Example

How to use:

When the application loads an ArcGISDynamicMapServiceLayer with several sub-layers will be display. For each sub-layer a CheckBox will be displayed in the ListBox. If the CheckBox is checked the sub-layer is visible. Interact with the CheckBoxes (checking them on/off) and then click one of the two buttons to re-draw the Map. Each of the two buttons performs the same end result but uses different way to accomplish drawing the sub-layer's visibility.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Changing the sub-layer visibility of an ArcGISDynamicMapServiceLayer.

XAMLCopy Code
<Grid x:Name="LayoutRoot">
  
  <!-- Add a Map Control and zoom into an area of interest that shows useful information. -->
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="12,168,0,0" Name="Map1" VerticalAlignment="Top" 
            Height="300" Width="500" Extent="-85.8005855980546,38.2488182089149,-85.7942764427118,38.2526037021206">
    
    <!-- Add an ArcGISDynamicMapServiceLayer. Wire up an Initialized Event to get the visibility for the 
         individual sub-layers that users can tweak to modify the results.-->
    <esri:ArcGISDynamicMapServiceLayer ID="Louisville"
          Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/MapServer"
          Initialized="ArcGISDynamicMapServiceLayer_Initialized"/>
    
  </esri:Map>
  
  <!-- Provide some GUI instructions to the user. -->
  <sdk:Label Height="25" HorizontalAlignment="Left" Margin="12,87,0,0" Name="Label1" VerticalAlignment="Top" 
             Width="284" Content="Click either button to apply the sub-layer visibility"/>
  
  <!-- This button will set the sub-layer visibility changes using the SetLayerVisibility Method. -->
  <Button Content="SetLayerVisibility Method" Height="23" HorizontalAlignment="Left" Margin="12,108,0,0" 
          Name="Button_SetLayerVisibility" VerticalAlignment="Top" Width="284" Click="Button_SetLayerVisibility_Click" />
  
  <!-- This button will set the sub-layer visibility changes using the VisibleLayers Property. -->
  <Button Content="VisibleLayers Property" Height="23" HorizontalAlignment="Left" Margin="12,139,0,0" 
          Name="Button_VisibleLayers" VerticalAlignment="Top" Width="284" Click="Button_VisibleLayers_Click"/>
  
  <!-- Add a ListBox to display each sub-layer visibility. Embedded in each row of the ListBox will be 
       a CheckBox (one for each sub-layer) that users can check on/off and then click one of the two buttons 
       to apply the changes. -->
  <ListBox Height="75" HorizontalAlignment="Left" Margin="302,87,0,0" Name="ListBox1" 
           VerticalAlignment="Top" Width="210" />
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="81" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="640" TextWrapping="Wrap" 
             Text="When the application loads an ArcGISDynamicMapServiceLayer with several sub-layers will be display. For each
             sub-layer a CheckBox will be displayed in the ListBox. If the CheckBox is checked the sub-layer is visible.
             Interact with the CheckBoxes (checking them on/off) and then click one of the two buttons to re-draw the
             Map. Each of the two buttons performs the same end result but uses different way to accomplish drawing
             the sub-layer's visibility." />
  
</Grid>
C#Copy Code
private void ArcGISDynamicMapServiceLayer_Initialized(object sender, System.EventArgs e)
{
  // When the ArcGISDynamicMapServiceLayer Initializes get the visibility for each sub-layer. 
  // It is necessary to use the ArcGISDynamicMapServiceLayer.GetLayerVisibility Method to obtain the 
  // visibility for each sub-layer of the web service on ArcGIS Server. 
  // NOTE: You can not use the ArcGISDynamicMapServiceLayer.VisibleLayers Property to obtain these values 
  // for the web service this the Property will always return null/Nothing. 
  
  // Get the ArcGISDynamicMapServiceLayer from the Map Control based upon its position in the Control.
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)(Map1.Layers[0]);
  
  // Get the count of the number of sub-layers in the ArcGISDynamicMapServiceLayer. 
  int numLayers = myArcGISDynamicMapServiceLayer.Layers.Length;
  
  // Loop through each sub-layer.
  for (var i = 0; i < numLayers; i++)
  {
    // Get the sub-layer visibility.
    bool myLayerVisibility = myArcGISDynamicMapServiceLayer.GetLayerVisibility(i);
    
    // Create a StackPanel to hold other Framework Element Controls.
    StackPanel myStackPanel = new StackPanel();
    myStackPanel.Orientation = Orientation.Horizontal;
    
    // Create a CheckBox to display if the sub-layer is visible or not.
    // Note the sub-layer ID is embedded as the .Tag for the CheckBox.
    CheckBox myCheckBox = new CheckBox();
    myCheckBox.Content = "Sub-layer ID: " + i.ToString();
    myCheckBox.IsChecked = myLayerVisibility;
    myCheckBox.Tag = i;
    myStackPanel.Children.Add(myCheckBox);
    
    // Add the StackPanel into the ListBox.
    ListBox1.Items.Add(myStackPanel);
  }
}
            
private void Button_SetLayerVisibility_Click(object sender, System.Windows.RoutedEventArgs e)
{
  
  // This function will apply the user changes to the sub-layer visibility to produce a new map
  // with different features displayed. This function uses the ArcGISDynamicMapServiceLayer.SetLayerVisibility
  // Method to control which sub-layers to draw.
  
  // Get the ArcGISDynamicMapServiceLayer from the Map Control based upon its position in the Control.
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)(Map1.Layers[0]);
  
  // Loop through each StackPanel in the ListBox.
  foreach (StackPanel myStackPanel in ListBox1.Items)
  {
    // Get the relevent information about the sub-layer visibility from the CheckBox.
    CheckBox myCheckBox = (CheckBox)(myStackPanel.Children[0]);
    int mySubLayerID = Convert.ToInt32(myCheckBox.Tag);
    bool myVisibility = Convert.ToBoolean(myCheckBox.IsChecked);
    
    // Use the SetLayerVisibility Method to control which sub-layers are drawn.
    myArcGISDynamicMapServiceLayer.SetLayerVisibility(mySubLayerID, myVisibility);
  }
  // Need to invoke the Refresh Method to redraw the sub-layer in the ArcGISDynamicMapServiceLayer.
  myArcGISDynamicMapServiceLayer.Refresh();
}
            
private void Button_VisibleLayers_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // This function will apply the user changes to the sub-layer visibility to produce a new map
  // with different features displayed. This function uses the ArcGISDynamicMapServiceLayer.VisibleLayers
  // Property to control which sub-layers to draw.
  
  // Get the ArcGISDynamicMapServiceLayer from the Map Control based upon its position in the Control.
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)(Map1.Layers[0]);
  
  // Create a List to hold which sub-layers are visible. This will be used later to construct an array.
  List<int> myList = new List<int>();
  
  // Loop through each StackPanel in the ListBox.
  foreach (StackPanel myStackPanel in ListBox1.Items)
  {
    // Get the relevent information about the sub-layer visibility from the CheckBox.
    CheckBox myCheckBox = (CheckBox)(myStackPanel.Children[0]);
    if (myCheckBox.IsChecked == true)
    {
      myList.Add(Convert.ToInt32(myCheckBox.Tag));
    }
  }
  
  // Create an Integer array from the List.
  int[] myVisibleLayers = myList.ToArray();
  
  // Use the VisibleLayers Propert to control which sub-layers are drawn.
  myArcGISDynamicMapServiceLayer.VisibleLayers = myVisibleLayers;
  
  // Need to invoke the Refresh Method to redraw the sub-layer in the ArcGISDynamicMapServiceLayer.
  myArcGISDynamicMapServiceLayer.Refresh();
}
VB.NETCopy Code
Private Sub ArcGISDynamicMapServiceLayer_Initialized(sender As System.Object, e As System.EventArgs)
  
  ' When the ArcGISDynamicMapServiceLayer Initializes get the visibility for each sub-layer. 
  ' It is necessary to use the ArcGISDynamicMapServiceLayer.GetLayerVisibility Method to obtain the 
  ' visibility for each sub-layer of the web service on ArcGIS Server. 
  ' NOTE: You can not use the ArcGISDynamicMapServiceLayer.VisibleLayers Property to obtain these values 
  ' for the web service this the Property will always return null/Nothing. 
  
  ' Get the ArcGISDynamicMapServiceLayer from the Map Control based upon its position in the Control.
  Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = CType(Map1.Layers.Item(0), ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
  
  ' Get the count of the number of sub-layers in the ArcGISDynamicMapServiceLayer. 
  Dim numLayers As Integer = myArcGISDynamicMapServiceLayer.Layers.Length
  
  ' Loop through each sub-layer.
  For i = 0 To numLayers - 1
    
    ' Get the sub-layer visibility.
    Dim myLayerVisibility As Boolean = myArcGISDynamicMapServiceLayer.GetLayerVisibility(i)
    
    ' Create a StackPanel to hold other Framework Element Controls.
    Dim myStackPanel As New StackPanel
    myStackPanel.Orientation = Orientation.Horizontal
    
    ' Create a CheckBox to display if the sub-layer is visible or not.
    ' Note the sub-layer ID is embedded as the .Tag for the CheckBox.
    Dim myCheckBox As New CheckBox
    myCheckBox.Content = "Sub-layer ID: " + i.ToString
    myCheckBox.IsChecked = myLayerVisibility
    myCheckBox.Tag = i
    myStackPanel.Children.Add(myCheckBox)
    
    ' Add the StackPanel into the ListBox.
    ListBox1.Items.Add(myStackPanel)
    
  Next
  
End Sub
            
Private Sub Button_SetLayerVisibility_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    
  ' This function will apply the user changes to the sub-layer visibility to produce a new map
  ' with different features displayed. This function uses the ArcGISDynamicMapServiceLayer.SetLayerVisibility
  ' Method to control which sub-layers to draw.
   
  ' Get the ArcGISDynamicMapServiceLayer from the Map Control based upon its position in the Control.
  Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = CType(Map1.Layers.Item(0), ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
  
  ' Loop through each StackPanel in the ListBox.
  For Each myStackPanel As StackPanel In ListBox1.Items
    
    ' Get the relevent information about the sub-layer visibility from the CheckBox.
    Dim myCheckBox As CheckBox = CType(myStackPanel.Children.Item(0), CheckBox)
    Dim mySubLayerID As Integer = CInt(myCheckBox.Tag)
    Dim myVisibility As Boolean = CBool(myCheckBox.IsChecked)
    
    ' Use the SetLayerVisibility Method to control which sub-layers are drawn.
    myArcGISDynamicMapServiceLayer.SetLayerVisibility(mySubLayerID, myVisibility)
    
  Next
  
  ' Need to invoke the Refresh Method to redraw the sub-layer in the ArcGISDynamicMapServiceLayer.
  myArcGISDynamicMapServiceLayer.Refresh()
  
End Sub
            
Private Sub Button_VisibleLayers_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' This function will apply the user changes to the sub-layer visibility to produce a new map
  ' with different features displayed. This function uses the ArcGISDynamicMapServiceLayer.VisibleLayers
  ' Property to control which sub-layers to draw.
  
  ' Get the ArcGISDynamicMapServiceLayer from the Map Control based upon its position in the Control.
  Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = CType(Map1.Layers.Item(0), ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
  
  ' Create a List to hold which sub-layers are visible. This will be used later to construct an array.
  Dim myList As New List(Of Integer)
  
  ' Loop through each StackPanel in the ListBox.
  For Each myStackPanel As StackPanel In ListBox1.Items
    
    ' Get the relevent information about the sub-layer visibility from the CheckBox.
    Dim myCheckBox As CheckBox = CType(myStackPanel.Children.Item(0), CheckBox)
    If myCheckBox.IsChecked = True Then
      myList.Add(CInt(myCheckBox.Tag))
    End If
    
  Next
  
  ' Create an Integer array from the List.
  Dim myVisibleLayers() As Integer = myList.ToArray
  
  ' Use the VisibleLayers Propert to control which sub-layers are drawn.
  myArcGISDynamicMapServiceLayer.VisibleLayers = myVisibleLayers
  
  ' Need to invoke the Refresh Method to redraw the sub-layer in the ArcGISDynamicMapServiceLayer.
  myArcGISDynamicMapServiceLayer.Refresh()
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.