This sample includes a Map with and ArcGISLocalTiledLayer and an ArcGISLocalDynamicMapServiceLayer. A layer list shows the local sub layer names, a visibility check box, and a slider to adjust opacity. Each local layer can be toggled off and on and the opacity levels adjusted.
Element binding in XAML is used to populate the ListBox contents
and enable interactivity between layers and UI elements in
the layer list.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.LocalSubLayerList "
xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:esri = " http://schemas.esri.com/arcgis/client/2009 " >
< Grid x:Name = " LayoutRoot " >
< Grid.Resources >
< BooleanToVisibilityConverter x:Key = " BooleanToVisibilityConverter " />
< 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 = " -15000000,2000000,-7000000,8000000 " Background = " #FFE3E3E3 " MinimumResolution = " 2445.98490512499 " >
< esri : ArcGISLocalTiledLayer ID = " Shaded Relief " Path = " ..\\Data\\TPKs\\Topographic.tpk " />
< esri : ArcGISLocalDynamicMapServiceLayer ID = " USA " x:Name = " USA "
Path = " ..\\Data\\MPKS\\USCitiesStates.mpk " Initialized = " ArcGISLocalDynamicMapServiceLayer_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 USA service " FontWeight = " Bold " Foreground = " White " />
< ListBox x:Name = " List " ItemsSource = " {Binding ElementName=USA, 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 = " USA "
ClickMode = " Press " Click = " CheckBox_Click " />
</ StackPanel >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
</ StackPanel >
</ Border >
< ProgressBar x:Name = " MyProgressBar " IsIndeterminate = " True " VerticalAlignment = " Bottom " Width = " 200 " Height = " 20 " Margin = " 10 " Visibility = " {Binding Path=IsBusy, Converter={StaticResource BooleanToVisibilityConverter}} " > </ ProgressBar >
</ 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;
using ESRI.ArcGIS.Client.Local;
using System.ComponentModel;
namespace ArcGISWPFSDK
{
public partial class LocalSubLayerList : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isBusy = true ;
public bool IsBusy
{
get
{
return _isBusy;
}
set
{
_isBusy = value;
if (PropertyChanged != null )
{
PropertyChanged(this , new PropertyChangedEventArgs("IsBusy" ));
}
}
}
Dictionary<string , int > _layerDictionary = new Dictionary<string , int >();
public LocalSubLayerList()
{
InitializeComponent();
DataContext = this ;
MyMap.Layers.LayersInitialized += (o, evt) =>
{
IsBusy = false ;
};
}
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];
ArcGISLocalDynamicMapServiceLayer dynamicServiceLayer = MyMap.Layers[serviceName] as
ArcGISLocalDynamicMapServiceLayer;
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 ArcGISLocalDynamicMapServiceLayer_Initialized(object sender, EventArgs e)
{
ArcGISLocalDynamicMapServiceLayer dynamicServiceLayer =
sender as ArcGISLocalDynamicMapServiceLayer;
if (dynamicServiceLayer.VisibleLayers == null )
dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer);
}
private int [] GetDefaultVisibleLayers(ArcGISLocalDynamicMapServiceLayer 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
Imports ESRI.ArcGIS.Client.Local
Imports System.ComponentModel
Namespace ArcGISWPFSDK
Partial Public Class LocalSubLayerList
Inherits UserControl
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _isBusy As Boolean = True
Public Property IsBusy() As Boolean
Get
Return _isBusy
End Get
Set (value As Boolean )
_isBusy = value
RaiseEvent PropertyChanged(Me , New PropertyChangedEventArgs("IsBusy" ))
End Set
End Property
Private _layerDictionary As New Dictionary(Of String , Integer )()
Public Sub New ()
InitializeComponent()
AddHandler MyMap.Layers.LayersInitialized, AddressOf map_LayersInitialized
End Sub
Private Sub map_LayersInitialized(sender As Object , e As EventArgs)
DataContext = Me
IsBusy = False
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 ArcGISLocalDynamicMapServiceLayer = TryCast(MyMap.Layers(serviceName), ArcGISLocalDynamicMapServiceLayer)
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 ArcGISLocalDynamicMapServiceLayer_Initialized(sender As Object , e As EventArgs)
Dim dynamicServiceLayer As ArcGISLocalDynamicMapServiceLayer = TryCast(sender, ArcGISLocalDynamicMapServiceLayer)
If dynamicServiceLayer.VisibleLayers Is Nothing Then
dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer)
End If
End Sub
Private Function GetDefaultVisibleLayers(dynamicService As ArcGISLocalDynamicMapServiceLayer) 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