Dynamic Layers In Code Online

This sample demonstrates changing the rendering, changing the order of layers, and adding layers on the fly to an ArcGISDynamicMapServiceLayer. To use the sample, click on the buttons in the upper left corner of the application. In the code-behind, the ArcGISDynamicMapServiceLayer.LayerDrawingOptions and ArcGISDynamicMapServiceLayer.DynamicLayerInfos properties are used to apply the changes based on the button that was clicked. Clicking the Range Value Renderer or Unique Value Renderer button generates a renderer of the appropriate type, creates a new LayerDrawingOptions object and sets its Renderer property to the generated renderer, and then uses the LayerDrawingOptions property to apply the new renderer. Clicking the Change Layer Order button clears the layer's LayerDrawingOptions by setting it to null uses DynamicLayerInfoCollection.RemoveAt and DynamicLayerInfoCollection.Add to move a layer. Clicking the Add Layer button gets a layer using the TableDataSource and adds it to the map using the DynamicLayerInfos.

Download Sample Application
XAML C# VB.NET
<UserControl x:Class="ArcGISWPFSDK.DynamicLayersInCode"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
    <Grid x:Name="LayoutRoot" Background="#FFE3E3E3">
        <Grid.Resources>
            <LinearGradientBrush x:Key="PanelGradient" EndPoint="0.5,1" StartPoint="0.5,0">
                <LinearGradientBrush.RelativeTransform>
                    <TransformGroup>
                        <ScaleTransform CenterY="0.5" CenterX="0.5"/>
                        <SkewTransform CenterY="0.5" CenterX="0.5"/>
                        <RotateTransform Angle="176" CenterY="0.5" CenterX="0.5"/>
                        <TranslateTransform/>
                    </TransformGroup>
                </LinearGradientBrush.RelativeTransform>
                <GradientStop Color="#FF145787" Offset="0.16"/>
                <GradientStop Color="#FF3D7FAC" Offset="0.502"/>
                <GradientStop Color="#FF88C5EF" Offset="0.984"/>
            </LinearGradientBrush>
        </Grid.Resources>
        <esri:Map x:Name="MyMap">
            <esri:Map.Extent>
                <esri:Envelope XMin="-3548912" YMin="-1847469" XMax="2472012" YMax="1742990">
                    <esri:Envelope.SpatialReference>
                        <esri:SpatialReference WKID="102009"/>
                    </esri:Envelope.SpatialReference>
                </esri:Envelope>
            </esri:Map.Extent>
            <esri:ArcGISDynamicMapServiceLayer ID="USA"				
				Url="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer" />
        </esri:Map>
        <StackPanel Orientation="Vertical" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Top">
            <Grid x:Name="DynamicLayersUiGrid" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,5,5,0">
                <Rectangle Fill="{StaticResource PanelGradient}" Stroke="Gray" Margin="0,0,0,0">
                    <Rectangle.Effect>
                        <DropShadowEffect/>
                    </Rectangle.Effect>
                </Rectangle>
                <Rectangle Margin="5" RadiusX="5" RadiusY="5" Fill="#DDFFFFFF" Stroke="DarkGray"/>
                <StackPanel Orientation="Vertical" VerticalAlignment="Top">
                    <Button Content="Range Value Renderer" Click="ApplyRangeValueClick" Width="140" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,10,5"/>
                    <Button Content="Unique Value Renderer" Click="ApplyUniqueValueClick" Width="140" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,10,5"/>
                    <Button Content="Change Layer Order" Click="ChangeLayerOrderClick" Width="140" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,10,5"/>
            <Button Content="Add Layer" Click="AddLayerClick" Width="140" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,10,10"/>
        </StackPanel>
            </Grid>
        </StackPanel>
    </Grid>
</UserControl>

Sample code usage restrictions
5/16/2014