Layer List

Download Samples Repository

Description

Shows how to access layers in the map.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl
    x:Class="ArcGISRuntime.Samples.Desktop.LayerList"
    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/runtime/2013">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <esri:MapView x:Name="MyMapView" Grid.Column="1">
			<esri:Map InitialViewpoint="-13279586, 4010136, -12786147, 4280850, 102100">
                <esri:ArcGISTiledMapServiceLayer DisplayName="Basemap"
				    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer" />

                <esri:ArcGISDynamicMapServiceLayer DisplayName="Fire Potential" Opacity=".35"
                    ServiceUri="http://maps1.arcgisonline.com/ArcGIS/rest/services/RMRS_Wildland_Fire_Potential/MapServer" />

                <esri:FeatureLayer DisplayName="Fire Perimeter">
                    <esri:FeatureLayer.FeatureTable>
                        <esri:ServiceFeatureTable
                            ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/2" />
                    </esri:FeatureLayer.FeatureTable>
                </esri:FeatureLayer>
                <esri:FeatureLayer DisplayName="Evacuation Perimeter">
                    <esri:FeatureLayer.FeatureTable>
                        <esri:ServiceFeatureTable
                            ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/1" />
                    </esri:FeatureLayer.FeatureTable>
                </esri:FeatureLayer>
                <esri:FeatureLayer DisplayName="Points of Interest">
                    <esri:FeatureLayer.FeatureTable>
                        <esri:ServiceFeatureTable
                            ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0" />
                    </esri:FeatureLayer.FeatureTable>
                </esri:FeatureLayer>
            </esri:Map>
        </esri:MapView>

        <Border Background="LightGray" BorderBrush="Black" BorderThickness="0,0,2,0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock Margin="20" FontSize="12pt">
                <Run Text="Map Layers" FontWeight="Bold" />
                <Run Text=" (drag layers to reorder)" />
                </TextBlock>

                <ListView x:Name="legend" Grid.Row="1" BorderBrush="Transparent" Background="Transparent"
						  ItemsSource="{Binding LegendLayers}"
						  AllowDrop="True" DragEnter="legend_DragEnter" Drop="legend_Drop"
						  PreviewMouseMove="legend_PreviewMouseMove" PreviewMouseLeftButtonDown="legend_PreviewMouseLeftButtonDown">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListView.ItemContainerStyle>

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Border BorderBrush="DarkGray" BorderThickness="2" Padding="4">
                                <StackPanel>
                                    <TextBlock Text="{Binding DisplayName}" FontSize="18" FontWeight="Bold" Margin="0,4" />
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <StackPanel>
                                            <CheckBox Content="Enabled" IsChecked="{Binding IsVisible, Mode=TwoWay}" Margin="6,2"/>
                                            <Slider Margin="0,0,20,0" Value="{Binding Opacity, Mode=TwoWay}" TickFrequency="0.01" Minimum="0" Maximum="1" />
                                        </StackPanel>
                                        <Button Grid.Column="1" Content="Remove" Padding="12,4" VerticalAlignment="Center" Click="RemoveLayerButton_Click" />
                                    </Grid>
                                </StackPanel>
                            </Border>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Layers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Shows how to access layers in the map.
    /// </summary>
    /// <title>Layer List</title>
    /// <category>Mapping</category>
    public sealed partial class LayerList : UserControl, INotifyPropertyChanged
    {
        private Point _startPoint;

		public IEnumerable<Layer> LegendLayers
		{
			get { return MyMapView.Map.Layers.Reverse(); }
		}
		
        public LayerList()
        {
            this.InitializeComponent();
			DataContext = this;
		}

        private void RemoveLayerButton_Click(object sender, RoutedEventArgs e)
        {
            var layer = (sender as FrameworkElement).DataContext as Layer;
            MyMapView.Map.Layers.Remove(layer);
			OnPropertyChanged("LegendLayers");
		}

        private void legend_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _startPoint = e.GetPosition(null);
        }

        private void legend_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.OriginalSource is Thumb)
                return;

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point position = e.GetPosition(null);

                if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    var layer = ((FrameworkElement)e.OriginalSource).DataContext as Layer;
                    if (layer == null)
                        return;

                    DataObject data = new DataObject("legendLayerFormat", layer);
                    DragDropEffects de = DragDrop.DoDragDrop(legend, data, DragDropEffects.Move);
                }
            }
        }

        private void legend_DragEnter(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent("legendLayerFormat") || sender == e.Source)
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void legend_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("legendLayerFormat"))
            {
                Layer moveLayer = e.Data.GetData("legendLayerFormat") as Layer;

                var lvItem = legend.ContainerFromElement((FrameworkElement)e.OriginalSource) as ListViewItem;
                if (lvItem != null)
                {
                    Layer replaceLayer = lvItem.DataContext as Layer;
                    if (replaceLayer != null)
                    {
                        int index = MyMapView.Map.Layers.IndexOf(replaceLayer);
                        if (index >= 0)
                        {
                            MyMapView.Map.Layers.Remove(moveLayer);
                            MyMapView.Map.Layers.Insert(index, moveLayer);
                        }
                        else
                        {
                            MyMapView.Map.Layers.Remove(moveLayer);
                            MyMapView.Map.Layers.Add(moveLayer);
                        }

						OnPropertyChanged("LegendLayers");
                    }
                }
            }
        }

		public event PropertyChangedEventHandler PropertyChanged = delegate { };
		private void OnPropertyChanged([CallerMemberName] string name = null)
		{
			PropertyChanged(this, new PropertyChangedEventArgs(name));
		}
	}
}
Feedback on this topic?