This sample contains a Map with one tiled map service layer and one
dynamic map service layer. A layer list shows the layers
(sublayers) within the dynamic map service and a visibility check
box. Element binding in XAML is used to populate the ListBox
contents and enable runtime interactivity between sublayers and UI
elements in the layer list.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.SubLayerList "
xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:esri = " clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client " >
< Grid x:Name = " LayoutRoot " >
< Grid.Resources >
< LinearGradientBrush x:Key = " PanelGradient " EndPoint = " 0.5,1 " StartPoint = " 0.5,0 " >
< LinearGradientBrush.RelativeTransform >
< TransformGroup >
< ScaleTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< SkewTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< RotateTransform Angle = " 176 " CenterY = " 0.5 " CenterX = " 0.5 " />
< TranslateTransform />
</ TransformGroup >
</ LinearGradientBrush.RelativeTransform >
< GradientStop Color = " #FF145787 " Offset = " 0.16 " />
< GradientStop Color = " #FF3D7FAC " Offset = " 0.502 " />
< GradientStop Color = " #FF88C5EF " Offset = " 0.984 " />
</ LinearGradientBrush >
</ Grid.Resources >
< esri : Map x:Name = " MyMap " WrapAround = " True " Extent = " -14930991.170,3611744.037,-11348896.882,5340571.181 " >
< esri : ArcGISTiledMapServiceLayer ID = " Street Map "
Url = " http://services.arcgisonline.com/ArcGIS/rest/services/World_Shaded_Relief/MapServer " />
< esri : ArcGISDynamicMapServiceLayer ID = " DynamicLayerCalifornia " x:Name = " DynamicLayerCalifornia "
Url = " http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer " Initialized = " ArcGISDynamicMapServiceLayer_Initialized " />
</ esri : Map >
< Border Background = " { StaticResource PanelGradient} " BorderThickness = " 1 " CornerRadius = " 5 "
HorizontalAlignment = " Right " VerticalAlignment = " Top "
Margin = " 20 " Padding = " 10 " BorderBrush = " Black " >
< StackPanel >
< TextBlock Text = " Layers in California service " FontWeight = " Bold " Foreground = " White " />
< ListBox x:Name = " List " ItemsSource = " {Binding ElementName=DynamicLayerCalifornia, Path=Layers, Mode=OneWay} " Background = " #DDFFFFFF " >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel Orientation = " Horizontal " >
< CheckBox Margin = " 2 " Content = " {Binding Name, Mode=OneWay} "
IsChecked = " {Binding DefaultVisibility, Mode=OneWay} "
Tag = " DynamicLayerCalifornia "
ClickMode = " Press " Click = " CheckBox_Click " />
</ StackPanel >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
</ StackPanel >
</ Border >
</ Grid >
</ UserControl >
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ArcGISWPFSDK
{
public partial class SubLayerList : UserControl
{
Dictionary<string , int > _layerDictionary = new Dictionary<string , int >();
public SubLayerList()
{
InitializeComponent();
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox tickedCheckBox = sender as CheckBox;
string serviceName = tickedCheckBox.Tag.ToString();
bool visible = (bool )tickedCheckBox.IsChecked;
string layerName = (string )tickedCheckBox.Content;
int layerIndex = _layerDictionary[layerName];
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();
}
private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e)
{
ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer =
sender as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
if (dynamicServiceLayer.VisibleLayers == null )
dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(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++)
{
_layerDictionary.Add(layerInfoArray[index].Name, layerInfoArray[index].ID);
if (layerInfoArray[index].DefaultVisibility)
visibleLayerIDList.Add(index);
}
return visibleLayerIDList.ToArray();
}
}
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Namespace ArcGISWPFSDK
Partial Public Class SubLayerList
Inherits UserControl
Private _layerDictionary As New Dictionary(Of String , Integer )()
Public Sub New ()
InitializeComponent()
End Sub
Private Sub CheckBox_Click(sender As Object , e As RoutedEventArgs)
Dim tickedCheckBox As CheckBox = TryCast(sender, CheckBox)
Dim serviceName As String = tickedCheckBox.Tag.ToString()
Dim visible As Boolean = CBool (tickedCheckBox.IsChecked)
Dim layerName As String = DirectCast (tickedCheckBox.Content, String )
Dim layerIndex As Integer = _layerDictionary(layerName)
Dim dynamicServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = TryCast(MyMap.Layers(serviceName), ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
Dim visibleLayerList As List(Of Integer ) = If (dynamicServiceLayer.VisibleLayers IsNot Nothing , dynamicServiceLayer.VisibleLayers.ToList(), New List(Of Integer )())
If visible Then
If Not visibleLayerList.Contains(layerIndex) Then
visibleLayerList.Add(layerIndex)
End If
Else
If visibleLayerList.Contains(layerIndex) Then
visibleLayerList.Remove(layerIndex)
End If
End If
dynamicServiceLayer.VisibleLayers = visibleLayerList.ToArray()
End Sub
Private Sub ArcGISDynamicMapServiceLayer_Initialized(sender As Object , e As EventArgs)
Dim dynamicServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = TryCast(sender, ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
If dynamicServiceLayer.VisibleLayers Is Nothing Then
dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer)
End If
End Sub
Private Function GetDefaultVisibleLayers(dynamicService As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer) As Integer ()
Dim visibleLayerIDList As New List(Of Integer )()
Dim layerInfoArray As ESRI.ArcGIS.Client.LayerInfo() = dynamicService.Layers
For index As Integer = 0 To layerInfoArray.Length - 1
_layerDictionary.Add(layerInfoArray(index).Name, layerInfoArray(index).ID)
If layerInfoArray(index).DefaultVisibility Then
visibleLayerIDList.Add(index)
End If
Next
Return visibleLayerIDList.ToArray()
End Function
End Class
End Namespace
5/16/2014