We've have several requests recently for the best way to bind the layers in the Map Contents panel to a ComboBox/ListBox/etc. One of the easiest ways is to use MapApplicationBindingSource in your xaml which then provides access to the Map (and layers). There are a couple issues you may encounter when binding the Map Contents to a control: (1) all layers will show up in the list, regardless of whether or not they???re meant to be hidden from Map Contents (i.e. if LayerName is null) and (2) the layers will appear in reverse order of how they draw on the map. As such, we've included a simple converter that removes any layers where the LayerName is null and reorders the layers. Both Xaml and C# code are included in this post (derived from the Viewer template in VS). Note there is no code in code-behind. Hope this helps. Katy<UserControl x:Class="DropDownMenuZoom.AddIns.SelectLayerDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:esri="http://schemas.esri.com/arcgis/client/extensibility/2010"
xmlns:local="clr-namespace:DropDownMenuZoom.AddIns"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<esri:MapApplicationBindingSource x:Key="MapApplication"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Resources>
<local:LayerToComboBoxConverter x:Name="LayerToComboBox" />
</Grid.Resources>
<StackPanel Margin="10" Background="Transparent">
<TextBlock Text="Select Layer:" Margin="0,0,0,5"/>
<ComboBox Height="23" Name="comboBox1" Width="200" ItemsSource="{Binding Map.Layers, Source={StaticResource MapApplication}, Converter={StaticResource LayerToComboBox}}"
DisplayMemberPath="LayerName">
</ComboBox>
</StackPanel>
</Grid>
</UserControl>
namespace DropDownMenuZoom.AddIns
{
[Export(typeof(ICommand))]
[DisplayName("Select Layer and Zoom")]
public class SelectLayer : ICommand
{
private SelectLayerDialog selectLayerDlg = new SelectLayerDialog();
#region ICommand members
public void Execute(object parameter)
{
// Show the configuration data. Use ShowWindow instead of MessageBox.
// There is a bug with Firefox 3.6 that crashes Silverlight when using
// MessageBox.Show.
MapApplication.Current.ShowWindow("Select Layer", selectLayerDlg);
selectLayerDlg.comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
}
void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Layer selectedLayer = selectLayerDlg.comboBox1.SelectedItem as Layer;
MapApplication.Current.Map.ZoomTo(selectedLayer.FullExtent);
}
public bool CanExecute(object parameter)
{
// Return true so that the command can always be executed
return true;
}
public event EventHandler CanExecuteChanged;
#endregion
}
public class LayerToComboBoxConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
LayerCollection lyrCollection = value as LayerCollection;
List<Layer> layers = new List<Layer>();
foreach (Layer lyr in lyrCollection)
{
if (!string.IsNullOrEmpty(MapApplication.GetLayerName(lyr)))
{
layers.Add(lyr);
}
}
layers.Reverse();
return layers;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Katy Dalton | Technical Consultant
THE SCIENCE OF WHERE™