<ComboBox x:Name="Layers" Width="150" Height="30" MaxDropDownHeight="150" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,5,0,5" > <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding LayerInfo.Name}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
public MainPage() { InitializeComponent(); string baseUrl = "http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer/"; for (int i = 0; i <= 12; i++) { FeatureLayer featureLayer = new FeatureLayer() { Url = string.Format("{0}/{1}", baseUrl, i) }; featureLayer.Initialize(); layers.Add(featureLayer); } Layers.ItemsSource = layers; Layers.SelectedIndex = 0;
<?xml version="1.0" encoding="utf-8"?> <cateogries> <Category Name="Airports"/> <Category Name="Cities"/> <Category Name="EarthquakeHistory"/> <Category Name="GolfCourses"/> <Category Name="Places"/> </cateogries>
<UserControl.Resources> <esri:DictionaryConverter x:Key="DictionaryConverter1"/> <esri:DictionaryConverter x:Key="DictionaryConverter"/> <DataTemplate x:Key="CategoryTemplate"> <StackPanel> <TextBlock Text="{Binding Name}"/> </StackPanel> </DataTemplate> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource testSampleDataSource}}"> <esri:Map x:Name="MyMap" Background="White" Margin="166,0,0,0"> <esri:Map.Extent> <esri:Envelope XMax="-131" XMin="-106" YMax="43" YMin="30"/> </esri:Map.Extent> <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com:80/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer" ID="tiled"/> <esri:ArcGISDynamicMapServiceLayer Url="http://serverapps.esri.com:80/arcgis/rest/services/California/MapServer" VisibleLayers="7,8,9,10" ID="dynam"/> </esri:Map> <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="42,0,0,17" VerticalAlignment="Bottom" Width="120" ItemsSource="{Binding CategoryCollection}" ItemTemplate="{StaticResource CategoryTemplate}" SelectionChanged="comboBox_SelectionChanged"/> </Grid> </UserControl>
InitializeComponent(); this.SubLayerComboBox.ItemsSource = layers; } ObservableCollection<LayerInfo> layers = new ObservableCollection<LayerInfo>(); ArcGISDynamicMapServiceLayer dynamicLayer; private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e) { dynamicLayer = sender as ArcGISDynamicMapServiceLayer; foreach (var l in dynamicLayer.Layers) layers.Add(l); } private void SubLayerComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { LayerInfo selected = this.SubLayerComboBox.SelectedItem as LayerInfo; if (dynamicLayer == null || selected == null) return; dynamicLayer.VisibleLayers = new int[] { selected.ID }; }
<ComboBox x:Name="SubLayerComboBox" VerticalAlignment="Top" HorizontalAlignment="Center" SelectionChanged="SubLayerComboBox_SelectionChanged"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
After the ArcGISDynamicMapServiceLayer has initialized, you can populate your list of LayerInfo. Your ComboBox could contain LayerInfo objects instead of hardcoding the Categories. In your SelectionChanged event, you can update the ArcGISDynamicMapServiceLayer's VisibleLayers property. This will show only the selected sub layer.
Code-behind:InitializeComponent(); this.SubLayerComboBox.ItemsSource = layers; } ObservableCollection<LayerInfo> layers = new ObservableCollection<LayerInfo>(); ArcGISDynamicMapServiceLayer dynamicLayer; private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e) { dynamicLayer = sender as ArcGISDynamicMapServiceLayer; foreach (var l in dynamicLayer.Layers) layers.Add(l); } private void SubLayerComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { LayerInfo selected = this.SubLayerComboBox.SelectedItem as LayerInfo; if (dynamicLayer == null || selected == null) return; dynamicLayer.VisibleLayers = new int[] { selected.ID }; }
You can then update your ComboBox ItemTemplate so it can rely on Binding to the LayerInfo.Name.
XAML-code:<ComboBox x:Name="SubLayerComboBox" VerticalAlignment="Top" HorizontalAlignment="Center" SelectionChanged="SubLayerComboBox_SelectionChanged"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
LayerInfo selected1 = this.SubLayerComboBox1.SelectedItem as LayerInfo; LayerInfo selected2 = this.SubLayerComboBox1.SelectedItem as LayerInfo; LayerInfo selected3 = this.SubLayerComboBox1.SelectedItem as LayerInfo; dynamicLayer.VisibleLayers = new int[] { selected1.ID, selected2.ID, selected3.ID };