[GIS] Silverlight API Radio Buttons from a dynamic map service layer in the legend

arcgis-silverlight-api

I am new to programming I am using API 2.4, silverlight 4 to create my web application. I have a application that has several feature layers, a dynamic map service layer and a couple tiled map service layers. I would like to have the three layers in the dynamic map service layer to be toggled on and off by a radio buttons so only one layer is on at a time. Below is my current code – any help would be greatly appreciated:

MainPage.xaml:

                <esri:ArcGISDynamicMapServiceLayer ID="Attendance Areas"
                                                   Opacity=".5"
                                                   Url="http://webaddress"/>

                <esri:ArcGISTiledMapServiceLayer ID="Labels"
                                                 Url="http://webaddress"/>


                <esri:FeatureLayer ID="Schools" 
                                   MouseEnter="FeatureLayer_MouseEnter"
                                   MouseLeave="FeatureLayer_MouseLeave"
                                   Url="webaddress"
                                   OutFields="SCHNAME, ADDRESS, PHONE, URL"/>

                <esri:FeatureLayer ID="Schools Without Attendance Areas" 
                                   MouseEnter="FeatureLayer_MouseEnter"
                                   MouseLeave="FeatureLayer_MouseLeave"
                                   Visible="False"
                                   Url="http://webaddress"
                                   OutFields="SCHNAME, ADDRESS, PHONE, URL"/>




                <esri:GraphicsLayer ID="ResultsGraphicsLayer"/>
                <esri:GraphicsLayer ID="MyGraphicsLayer"/>
                <esri:GraphicsLayer ID="IdentifyIconGraphicsLayer"/>


                <esri:GraphicsLayer ID="AddressToLocationGraphicsLayer">
                    <esri:GraphicsLayer.MapTip>
                        <Grid Background="LightYellow">
                            <StackPanel Margin="5" >
                                <TextBlock Text="{Binding [Address]}" HorizontalAlignment="Left" />
                                <TextBlock Text="{Binding [LatLon]}" HorizontalAlignment="Left" />
                            </StackPanel>
                            <Border BorderBrush="Black" BorderThickness="1" />
                        </Grid>
                    </esri:GraphicsLayer.MapTip>
                </esri:GraphicsLayer>
            </esri:Map.Layers>
        </esri:Map>

App.xaml:

                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox Content="{Binding Label}"
                                        IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                                        IsEnabled="{Binding IsInScaleRange}" >
                                </CheckBox>

                            </StackPanel>
                        </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="MapLayerTemplate">
            <Setter.Value>

                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Content="{Binding Label}"
                                        IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                                        IsEnabled="{Binding IsInScaleRange}" >
                        </CheckBox>

                    </StackPanel>
                </DataTemplate>
            </Setter.Value>

            </Setter>
    </Style>

MainPage.cs:

        private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
    {
        LayerItemViewModel removeLayerItemVM = null;

        if (e.LayerItem.Layer is ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)
            e.LayerItem.LayerItems = null;

        if (e.LayerItem.LayerItems != null)
        {
            foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)
            {
                if (layerItemVM.IsExpanded)
                    layerItemVM.IsExpanded = false;


            }

            if (removeLayerItemVM != null)
                e.LayerItem.LayerItems.Remove(removeLayerItemVM);
        }
        else
        {
            e.LayerItem.IsExpanded = false;
        }
    }

From this post, http://forums.arcgis.com/threads/272…t=Radio+button, it seems like it is possible to toggle layers. I have 3 layer in a single service I would like to have toggle control on so only one layer is displayed at a time. I am just not sure how to go about doing that with the legend I have setup. Does any one have any sample code to get me started?

Later I added the following code into my above code to attempt to make the radio buttons work. I succeeded in having radio button in the legend for the layer i wanted but the layer name not longer displays…only when I expand the layer I see the symbology. Also click the radio button has no effect on the map. Please, Please help – I am so completely stuck:

App.xaml:

                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Checked">
                                    <i:InvokeCommandAction Command="{Binding RadioButtonCheckedCommand}"
                                                           CommandParameter="{Binding ElementName=rbLayer}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>

                        </RadioButton>

MainPage.xaml.cs:

 private void RadioButtonCheckedCommand(object sender, RoutedEventArgs e)
    {
        RadioButton tickedCheckBox = sender as RadioButton;
        string serviceName = tickedCheckBox.Name;
        bool visible = (bool)tickedCheckBox.IsChecked;
        int layerIndex = (int)tickedCheckBox.Tag;
        ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = MyMap.Layers[serviceName] as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
        dynamicServiceLayer.VisibleLayers = new int[] { layerIndex };
    }

Best Answer

I removed all the App.xaml code that referred to the legend and all code behind for the radio buttons (but I kept the Legend refreshed)

MainPage.xaml: Code:

        <userControls:DraggableWindow IsOpen="True" x:Name="MapLegendWindow" Margin="0,5,10,0" 
                                    HorizontalAlignment="Right" Width="311" Padding="0" 
                                    HorizontalContentAlignment="Stretch" 
                                    VerticalContentAlignment="Stretch" 
                                    Title="Map Legend" Background="{StaticResource BaseColor}" 
                                    VerticalAlignment="Top" Height="425">
            <i:Interaction.Triggers>
                <i:EventTrigger>
                    <!--<actions:ToggleWindowVisibilityAction />--> <!-- Hide at startup -->
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <esri:Legend x:Name="Legend" Map="{Binding ElementName=MyMap}"
                         LayerIDs="Labels, Public Schools, Non-Public Schools, Attendance Areas"
                         LayerItemsMode="Tree"
                         ShowOnlyVisibleLayers="False"
                         Refreshed="Legend_Refreshed" >

            <esri:Legend.MapLayerTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Content="{Binding Label}"
                                            IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                                            IsEnabled="{Binding IsInScaleRange}" />


                    </StackPanel>
                </DataTemplate>
            </esri:Legend.MapLayerTemplate>

            <esri:Legend.LayerTemplate>
                <DataTemplate>
                    <RadioButton Content="{Binding Label}"
                                 Background="#01FFFFFF"
                                 Foreground="Black"
                                 IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                                 IsEnabled="{Binding IsInScaleRange}" />

                </DataTemplate>
            </esri:Legend.LayerTemplate>
            </esri:Legend>
        </userControls:DraggableWindow>

MainPage.xaml.cs: Code:

    private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)
    {
        LayerItemViewModel removeLayerItemVM = null;


        if (e.LayerItem.Layer is ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)
            e.LayerItem.LayerItems = null;

        if (e.LayerItem.LayerItems != null)
        {
            foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)
            {
                if (layerItemVM.IsExpanded)
                    layerItemVM.IsExpanded = false;
            }

            if (removeLayerItemVM != null)
                e.LayerItem.LayerItems.Remove(removeLayerItemVM);
        }
        else
        {
            e.LayerItem.IsExpanded = true;
        }
    }