Select to view content in your preferred language

How can I make a FeatureLayer appear in the Legend?

864
2
03-20-2014 11:06 AM
DonaldScott
Deactivated User
Hello,

I've added an <esri:FeatureLayer> to my map and I would like for the users to be able to click the <esri:Legend> to show and hide it. The Legend works fine with the ArcGISDynamicMapServiceLayer but it ignores the FeatureLayer. Is there a way to make the FeatureLayer appear in the Legend?

Thank you.


Here is a sample of my XAML showing the Legend and Map.

<!-- Legend -->
<Grid x:Name="MapLegendPanelGrid" ShowGridLines="False" Width="auto" Height="auto" 
      HorizontalAlignment="Left" Margin="0,10,0,0" Visibility="Collapsed">
 <Grid.RowDefinitions>
  <RowDefinition Height="25" />
  <RowDefinition Height="*" />
  <RowDefinition Height="Auto" />
  <RowDefinition Height="0"/>
  <RowDefinition Height="0"/>
 </Grid.RowDefinitions>
 <Grid.ColumnDefinitions>
  <ColumnDefinition />
 </Grid.ColumnDefinitions>
 <sdk:GridSplitter x:Name="grsplMapLegendPanelSplitter" Grid.Row="2" Grid.Column="0" 
                   VerticalAlignment="Center" HorizontalAlignment="Stretch" 
       Height="5" Background="{StaticResource BaseColor}">
 </sdk:GridSplitter>

 <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
  <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" FontWeight="Bold" Text="Layers" Margin="2,0,0,5" />
 </StackPanel>
 <ScrollViewer Width="395" BorderThickness="0" Height="Auto" HorizontalScrollBarVisibility="Disabled"  
     VerticalScrollBarVisibility="Auto" Grid.Row="1" Grid.Column="0">
  <esri:Legend Map="{Binding ElementName=Map}" Name="MapLegend" LayerItemsMode="Tree" 
               ShowOnlyVisibleLayers="False" Refreshed="MapLegend_Refreshed" 
      ScrollViewer.VerticalScrollBarVisibility="Hidden" Background="White" Width="390" Height="auto">
   <esri:Legend.MapLayerTemplate>
    <DataTemplate>
     <StackPanel Orientation="Horizontal">
      <CheckBox Content="{Binding Label}" Width="220" IsChecked="{Binding IsEnabled, Mode=TwoWay}" 
                IsEnabled="{Binding IsInScaleRange}"></CheckBox>
      <Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="50" />
     </StackPanel>
    </DataTemplate>
   </esri:Legend.MapLayerTemplate>
   <esri:Legend.LayerTemplate>
    <DataTemplate>
     <CheckBox Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" 
     IsEnabled="{Binding IsInScaleRange}"></CheckBox>
    </DataTemplate>
   </esri:Legend.LayerTemplate>
  </esri:Legend>
 </ScrollViewer>
</Grid>

    


<!-- Map -->
<esri:Map x:Name="Map" Background="{StaticResource MapBackgroundColor}" WrapAround="true" 
          MinimumResolution=".01" IsLogoVisible="False"  ExtentChanged="Map_ExtentChanged" 
    Grid.Column="1" SnapToLevels="True">
 <esri:Map.Extent>
  <esri:Envelope XMax="{StaticResource MapXMax}" XMin="{StaticResource MapXMin}" 
                 YMax="{StaticResource MapYMax}" YMin="{StaticResource MapYMin}">
   <esri:Envelope.SpatialReference>
    <esri:SpatialReference WKID="{StaticResource MapWKID}" />
   </esri:Envelope.SpatialReference>
  </esri:Envelope>
 </esri:Map.Extent>
 <esri:Map.Layers>
  <esri:ArcGISTiledMapServiceLayer ID="ArcGISBaseLayerTiled" Url="{StaticResource BasemapUrlAerialUsa}" 
                                   MinimumResolution="0.224896283" InitializationFailed="Layer_InitializationFailed"  />

  <esri:ArcGISDynamicMapServiceLayer ID="ArcGISBaseLayerDynamic" Url="http://. . . mapservice" 
                                     MinimumResolution="0.224896283" InitializationFailed="Layer_InitializationFailed"  />
  
  <esri:FeatureLayer ID="MyFeatureLayer"
       Url="http://. . ./FeatureServer/0"
       MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp"
       DisableClientCaching="True" 
       Mode="OnDemand"
       SelectionColor="#FFFFFF00"
       OutFields="*"
       />
 </esri:Map.Layers>
</esri:Map>

0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor
The feature layers are supposed to show up in the legend as others layers.
The interactive SDK Legend Sample demonstrates an example of feature layer in the legend ('Points of Interest').

Might be you redefined the legend style in your application that would overwrite the default. Check in app.xaml.

Else could you provide a repro case with public services?

Thanks
0 Kudos
DonaldScott
Deactivated User
Dominique,

I see that you are correct.  I had these two lines of code which I did not clearly understand:
            this.MyLegend.LayerIDs = MyMapSeriviceIds;
            this.MyLegend.UpdateLayout();


When I removed them, the Legend showed it's default behavior, which is to show all layers.

Thank you very much for your help.

Best Regards,

Don
0 Kudos