Select to view content in your preferred language

Using a Combobox to change map and featurelayer URLs

2648
5
11-08-2010 11:32 AM
WilliamDonahue
Emerging Contributor
I am attampting to reference multiple maps to a single application. They all woulc consist of a mapserver and a single FeatureLayer which i would like to edit.

So far I have The following XAML

<ComboBox x:Name="combo_select" Height="33" Margin="57,405,48,0" VerticalAlignment="Top" SelectionChanged="SelectionChanged">
      <TextBlock Text="Standard Map" Tag="<url>/FeatureServer/0"/>
      <TextBlock Text="Map Variant 2" Tag="<url>/FeatureServer/0"/>
     </ComboBox>


esri:Map x:Name="Map" Background="White" Grid.Column="1" Margin="8" Grid.Row="1">
   <esri:ArcGISDynamicMapServiceLayer ID="Layers" Url="<url>/MapServer"/>
   <esri:FeatureLayer ID="CB" Url="<url>FeatureServer/0"
    MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp" DisableClientCaching="True" AutoSave="False"
    Mode="OnDemand" OutFields="*"/>


And for my C# code behind I have

private void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  {
  FeatureLayer arcgisLayer = Map.Layers["CB"] as FeatureLayer;
            arcgisLayer.Url = ((ComboBox)sender).Tag as string;


I based my code on the radio button model from the sample. MY problems are 2 fold. The first is I have set it up to just change the featureLayers currently, however the layers do not change. Also my map is based on slightly differnet base maps and moving foreward i do not see the ability to change both the map url and the FeatureLayer URL.
0 Kudos
5 Replies
JenniferNery
Esri Regular Contributor
The FeatureLayer Url property cannot be changed after the layer has been initialized.

I would suggest swapping out the layers themselves rather than Url's. Your ComboBox ItemsSource will then have list of FeatureLayers instead of string URL's. Just update its ItemTemplate to show the either the Url or LayerInfo.Name to identify the layer. On the ComboBox SelectionChanged event, you can add/remove layers to/from your Map.

  public MainPage()
  {
   InitializeComponent();

   FeatureLayer layer = new FeatureLayer() { Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/FeatureServer/0" };
   layer.Initialized += new System.EventHandler<System.EventArgs>(layer_Initialized);
   layer.Initialize();
   layer = new FeatureLayer() { Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/FeatureServer/1" };
   layer.Initialized += new System.EventHandler<System.EventArgs>(layer_Initialized);
   layer.Initialize();
   this.FeatureLayersCB.ItemsSource = layers;
  }

  ObservableCollection<FeatureLayer> layers = new ObservableCollection<FeatureLayer>();
  void layer_Initialized(object sender, System.EventArgs e)
  {
   layers.Add(sender as FeatureLayer);
  }


<ComboBox x:Name="FeatureLayersCB" VerticalAlignment="Top" HorizontalAlignment="Center">
   <ComboBox.ItemTemplate>
    <DataTemplate>
     <TextBlock Text="{Binding LayerInfo.Name}"/>
    </DataTemplate>
   </ComboBox.ItemTemplate>
  </ComboBox>
0 Kudos
WilliamDonahue
Emerging Contributor
I guess a better question would be. Seeing as how within the applications the primary difference is the symbology would it just be easier to use the script to change symbology for the map. My only problem would then be letting people know what each symbology means.
0 Kudos
WilliamDonahue
Emerging Contributor
Looking over the Samples library would it be possible for me to use the toggle layer visibility function and combine that with a way to change the layer that the editor is pointing at?
0 Kudos
JenniferNery
Esri Regular Contributor
The Editor has LayerIDs property you can update (http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Editor~Lay...). However, I can assure you that the Editor will not act on layers that are not visible 🙂
0 Kudos
WilliamDonahue
Emerging Contributor
Thanks for the help.

I assume that when i use the combo box to change the visible layers i would have to begin my code behind portion with a foreach loop to clear the visible layer first.
0 Kudos