Select to view content in your preferred language

How to display layers based on selected combobox item?Very urgent

2180
5
11-09-2010 09:26 AM
sibbaMaharaj
Emerging Contributor
HI everyone,i want a little help

problem is

i have a combo box and have these values
* Airports
* Cities
* EarthquakeHistory
* GolfCourses
* Places
* Volcanos
* Highways
* Rivers
* Counties
* Lakes
* StateBND
* UrbanAreas


as combox items and these are different layers on the server and the url for these items is
http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer

Now whenever i choose one of these values mymap should display those layers

for example if i choose cities i should get cities layer and if i choose countries i should countries layers

How to solve this problem?help me friends
0 Kudos
5 Replies
JMcNeil
Deactivated User
sibba,

i'm slightly confused on what you need.  Are you just trying to populate a combo box with layer names..?...if so it would be something like this.

In your xaml:

 <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>



In your code behind C#

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;





That will populate your combo box and bind it with your service. 


Now if you want to like turn off and on layers based on what is selected in the combo drop down then why not use the sublayer list example from the sdk

http://help.arcgis.com/en/webapi/silverlight/2.1/samples/start.htm#SubLayerList

or better yet the legend with templates

http://help.arcgis.com/en/webapi/silverlight/2.1/samples/start.htm#LegendWithTemplates

this is all 2.1 SDK if you want 2.0 you can use the sublayerlist but not legendwithtemplates.



Now if you really want to have the combo box drive the layer that is visiblie then use the code above to create the combo box and look at the sublayer list example..,....instead of binding visibility to the checkbox_click try setting it to SelectedIndex="" or SelectValue="" something like that.

J
0 Kudos
sibbaMaharaj
Emerging Contributor
i had imported xmldatasource to my project and used those items to pop up in combobox

This is my xmldata

<?xml version="1.0" encoding="utf-8"?>
<cateogries>
  <Category Name="Airports"/>
<Category Name="Cities"/>
<Category Name="EarthquakeHistory"/>
<Category Name="GolfCourses"/>
<Category Name="Places"/>
</cateogries>


this is my xaml code
 <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>


here testsampledatasource is my xmldatasource name categorycollection is where i get values and display in combobox


now tell me how can i display the layers based on combobox selection,please
0 Kudos
JenniferNery
Esri Regular Contributor
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>
0 Kudos
sibbaMaharaj
Emerging Contributor
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>



😉
thanks a lot for your fast response ,its working now ,one more doubt
if i have two comboboxes one with predefined values like A,B,C and other should populate based on the selected  values but from the server

if i have 9 feature layers in the server
like
1
2
3
4
5
6
7
8
9


and three different dependency values in other combobox like
A
B
C

this combobox should raise an event selectionchanged whenever any item is choosed ,based on this item my secondbox should populate with desired result like this

whenever i choose A i should get 1,2,3 layer names populated in combobox and
when i choose B i should get 4,5,6 layer names populated in combobox
and when i choose C i should get 7,8,9 layers populated in combobox

This is what i want


How is this done?
This is my college project ,please help me
0 Kudos
JenniferNery
Esri Regular Contributor
If you need to place the LayerInfo in three different containers, then you need three ObservableCollection<LayerInfo> too, where you add only a portion of the LayerInfos in each collection.

And if you need each selection to affect the VisibleLayers, you can do something like.
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 }; 
0 Kudos