Select to view content in your preferred language

Radio buttons to toggle layers from a Dynamic map service layer in the legend

3820
10
Jump to solution
09-13-2012 08:49 AM
TanyaOwens
Frequent Contributor
Hi,

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:Map x:Name="MyMap" Background="White" WrapAround="true" IsLogoVisible="False" Loaded="MyMap_Loaded" Extent="959677,402707,1075561,470675" MouseClick="MyMap_MouseClick">                 <esri:Map.Layers>                     <esri:ArcGISTiledMapServiceLayer ID="AGOLayer"                                                      Url="http://webaddress"/>                                          <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:
        <Style TargetType="esri:Legend">             <Setter Property="LayerItemsMode" Value="Flat" />             <Setter Property="LayerTemplate">                 <Setter.Value>                              <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.xaml.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;             }         }
0 Kudos
1 Solution

Accepted Solutions
TanyaOwens
Frequent Contributor
Ok - this is what I have now (See below for code) that works mostly except for 2 issues.
1. Even though one of the radio buttons is on when I start the app, none of the layers show until I click a different layer in the group then I can get them all to show

2. I would like to have the option of having them all turned off

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:
            <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="Flat"                              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:
        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;             }         }

View solution in original post

0 Kudos
10 Replies
TanyaOwens
Frequent Contributor
From this post, http://forums.arcgis.com/threads/27210-Can-I-use-radio-button-to-change-layers-in-the-same-map-servi..., 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?
0 Kudos
TanyaOwens
Frequent Contributor
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:

 <RadioButton x:Name="rbLayer"
                                         GroupName="AttendanceAreaLayers"
                                         IsChecked="{Binding DefaultVisibility}"
                                         DataContext="{Binding ElementName=Legend}"
                                         Content="{Binding Label}" 
                                         Background="#01FFFFFF">
                                         
                                

                                <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 };
        }
0 Kudos
TanyaOwens
Frequent Contributor
Well I just made an attempt to try to force radio buttons into the legend (see code below) and got the following error:

Line: 56
Error: Unhandled Error in Silverlight Application
Code: 4004   
Category: ManagedRuntimeError      
Message: System.ArgumentException: Service URL does not contain any layer.
   at ESRI.ArcGIS.Client.Layer.OnInitializationFailed(EventArgs e)
   at ESRI.ArcGIS.Client.Layer.Initialize()
   at ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer.<>n__FabricatedMethod16()
   at ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer.<>c__DisplayClass14.<MapServiceInfoInitFailed>b__12()    

MainPage.xaml:
                <!-- Map legend window -->
          <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="335">
                <i:Interaction.Triggers>
                    <i:EventTrigger>
                        <!--<actions:ToggleWindowVisibilityAction />--> <!-- Hide at startup -->
                    </i:EventTrigger>
                </i:Interaction.Triggers>

                <StackPanel HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Vertical" Width="262">

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

                
                    <RadioButton x:Name="ElementarySchoolAttendanceAreas" 
                         Tag="website/MapServer/0"
                         IsChecked="true" Margin="5,0,0,0" Foreground="White"
                         GroupName="LegendLayers" Content="Elementary School Attendance Areas" Click="LegendRadioButton_Click"/>

                    <RadioButton x:Name="MiddleSchoolAttendanceAreas" 
                         Tag="website/MapServer/1"
                         IsChecked="true" Margin="5,0,0,0" Foreground="White"
                         GroupName="LegendLayers" Content="Middle School Attendance Areas" Click="LegendRadioButton_Click"/>

                    <RadioButton x:Name="HighSchoolAttendanceAreas" 
                         Tag="website/MapServer/2"
                         IsChecked="true" Margin="5,0,0,0" Foreground="White"
                         GroupName="LegendLayers" Content="High School Attendance Areas" Click="LegendRadioButton_Click"/>

                </StackPanel>


            </userControls:DraggableWindow>


MainPage.xaml.cs:
        private void LegendRadioButton_Click(object sender, RoutedEventArgs e)
        {
            ArcGISDynamicMapServiceLayer arcgisLayer = MyMap.Layers["Elementary School Attendance Area"] as ArcGISDynamicMapServiceLayer;
            arcgisLayer.Url = ((RadioButton)sender).Tag as string;
        }
0 Kudos
TanyaOwens
Frequent Contributor
So I decided to move the legend style to the MainPage.xaml to see if anything would help (See code below). Now I get a different error at the "int layerIndx = (int)tickedCheckBox.Tag;" line in the code behind

The error:
System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  StackTrace:
       at PAGSchoolSearchLayout.MainPage.LegendRb_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Primitives.ToggleButton.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonDown(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonDown(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
  InnerException:

MainPage.xaml:

            <Style TargetType="esri:Legend">
                <Setter Property="LayerItemsMode" Value="Flat" />
                <Setter Property="LayerTemplate">
                    <Setter.Value>

                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
           
                                <RadioButton Name="DynamicLayerAttendanceArea"
                                         GroupName="DynamicLayerAttendanceAreas"
                                         Content="{Binding Name}"
                                         IsChecked="{Binding DefaultVisibility}"
                                         Tag="{Binding ID}"
                                         ClickMode="Press"
                                         Click="LegendRb_Click"                                                                              
                                         Background="#01FFFFFF">
                                </RadioButton>

                            </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.xaml.cs
        private void LegendRb_Click(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 };
        }


Any thoughts?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Well I just made an attempt to try to force radio buttons into the legend (see code below) and got the following error:

Line: 56
Error: Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.ArgumentException: Service URL does not contain any layer.
at ESRI.ArcGIS.Client.Layer.OnInitializationFailed(EventArgs e)
at ESRI.ArcGIS.Client.Layer.Initialize()
at ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer.<>n__FabricatedMethod16()
at ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer.<>c__DisplayClass14.<MapServiceInfoInitFailed>b__12()


Looks like there is a mismatch in your layerName.

Your radio button is called : ElementarySchoolAttendanceAreas
<RadioButton x:Name="ElementarySchoolAttendanceAreas"


Your layer name is supposed to have the same name, i.e ElementarySchoolAttendanceAreas
string serviceName = tickedCheckBox.Name;
ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = MyMap.Layers[serviceName] as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;


But your layer is called : Attendance Areas
<esri:ArcGISDynamicMapServiceLayer ID="Attendance Areas"


--> dynamicServiceLayer is null
and you get the exception on :
dynamicServiceLayer.VisibleLayers = new int[] { layerIndex };
0 Kudos
TanyaOwens
Frequent Contributor
Thank you, but is there a way to get the radio buttons to work in the original code...I would rather that than forcing it with a stack panel that doesn't work well with the draggable window and scroll bar.

I will work on this code in the meantime.
0 Kudos
TanyaOwens
Frequent Contributor
Ok - this is what I have now (See below for code) that works mostly except for 2 issues.
1. Even though one of the radio buttons is on when I start the app, none of the layers show until I click a different layer in the group then I can get them all to show

2. I would like to have the option of having them all turned off

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:
            <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="Flat"                              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:
        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;             }         }
0 Kudos
TanyaOwens
Frequent Contributor

1. Even though one of the radio buttons is on when I start the app, none of the layers show until I click a different layer in the group then I can get them all to show


I found out the reason behind this one but I don't know how to fix it. I have 2 tiled map service layers in addition to my dynamic layer and a couple feature layers. When I comment out the tiled map service layer for the labels the dynamic layer is on a start up; when I bring it back it causes the problem again...it also doesn't seem what order I have it in my map layers - I tried it before and after the dynamic layer used for the radio button but I have the same results. Any clue on how to fix this?

(I am still trying to figure out how to be able to all the layers turned off by the user.)

MainPage.xaml code for the map layers:
            <esri:Map x:Name="MyMap" Background="White" WrapAround="true" IsLogoVisible="False" Loaded="MyMap_Loaded" Extent="959677,402707,1075561,470675" 
                      MouseClick="MyMap_MouseClick">
                
                <esri:Map.Layers>
                    <esri:ArcGISTiledMapServiceLayer ID="AGOLayer"
                                                     Url="website address"/>

                    <esri:ArcGISDynamicMapServiceLayer ID="Attendance Areas"
                                                       Opacity=".4"                                                       
                                                       Url="website address"
                                                       VisibleLayers="0"/>


                    <esri:ArcGISTiledMapServiceLayer ID="Labels"
                                                     Url="website address"/>


                    <esri:FeatureLayer ID="Public Schools" 
                                       MouseEnter="FeatureLayer_MouseEnter"
                                       MouseLeave="FeatureLayer_MouseLeave"
                                       Url="website address"
                                       OutFields="SCHNAME, ADDRESS, PHONE, URL">
                                        <esri:FeatureLayer.MapTip>
                                            <Border CornerRadius="10" BorderBrush="#FF51626F" BorderThickness="2" Margin="0,0,15,15" Background="LightGray">
                                                <StackPanel Margin="7">
                                                    <StackPanel Orientation="Vertical">
                                                        <TextBlock Text="{Binding [SCHNAME]}" Foreground="Black" FontWeight="Bold"/>
                                                        <TextBlock Text="(Click point for more information) " Foreground="Black"/>
                                                    </StackPanel>
                                                </StackPanel>
                                            </Border>
                                        </esri:FeatureLayer.MapTip>
                    </esri:FeatureLayer>

                    <esri:FeatureLayer ID="Non-Public Schools" 
                                       MouseEnter="FeatureLayer_MouseEnter"
                                       MouseLeave="FeatureLayer_MouseLeave"
                                       Visible="False"
                                       Url="website address"
                                       OutFields="SCHNAME, ADDRESS, PHONE, URL">
                                        <esri:FeatureLayer.MapTip>
                                            <Border CornerRadius="10" BorderBrush="#FF51626F" BorderThickness="2" Margin="0,0,15,15" Background="LightGray">
                                                <StackPanel Margin="7">
                                                    <StackPanel Orientation="Vertical">
                                                        <TextBlock Text="{Binding [SCHNAME]}" Foreground="Black" FontWeight="Bold"/>
                                                        <TextBlock Text="(Click point for more information) " Foreground="Black"/>
                                                    </StackPanel>
                                                </StackPanel>
                                            </Border>
                                        </esri:FeatureLayer.MapTip>
                    </esri:FeatureLayer>



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


                    <esri:GraphicsLayer ID="AddressToLocationGraphicsLayer">
                        <esri:GraphicsLayer.MapTip>
                            <Grid Background="LightGray">
                                <StackPanel Margin="5" >
                                    <TextBlock Text="{Binding [Address]}" HorizontalAlignment="Left" />
                                </StackPanel>
                                <Border BorderBrush="#FF51626F" BorderThickness="1" />
                            </Grid>
                        </esri:GraphicsLayer.MapTip>
                    </esri:GraphicsLayer>
                </esri:Map.Layers>
            </esri:Map>
0 Kudos
TanyaOwens
Frequent Contributor
Ok - this is what I have now (See below for code) that works mostly except for 2 issues.
1. Even though one of the radio buttons is on when I start the app, none of the layers show until I click a different layer in the group then I can get them all to show

2. I would like to have the option of having them all turned off

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:
            <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="Flat"
                             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:
        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;
            }
        }


I found out the reason behind this one but I don't know how to fix it. I have 2 tiled map service layers in addition to my dynamic layer and a couple feature layers. When I comment out the tiled map service layer for the labels the dynamic layer is on a start up; when I bring it back it causes the problem again...it also doesn't seem what order I have it in my map layers - I tried it before and after the dynamic layer used for the radio button but I have the same results. Any clue on how to fix this?

(I am still trying to figure out how to be able to all the layers turned off by the user.)

MainPage.xaml code for the map layers:
            <esri:Map x:Name="MyMap" Background="White" WrapAround="true" IsLogoVisible="False" Loaded="MyMap_Loaded" Extent="959677,402707,1075561,470675" 
                      MouseClick="MyMap_MouseClick">
                
                <esri:Map.Layers>
                    <esri:ArcGISTiledMapServiceLayer ID="AGOLayer"
                                                     Url="website address"/>

                    <esri:ArcGISDynamicMapServiceLayer ID="Attendance Areas"
                                                       Opacity=".4"                                                       
                                                       Url="website address"
                                                       VisibleLayers="0"/>


                    <esri:ArcGISTiledMapServiceLayer ID="Labels"
                                                     Url="website address"/>


                    <esri:FeatureLayer ID="Public Schools" 
                                       MouseEnter="FeatureLayer_MouseEnter"
                                       MouseLeave="FeatureLayer_MouseLeave"
                                       Url="website address"
                                       OutFields="SCHNAME, ADDRESS, PHONE, URL">
                                        <esri:FeatureLayer.MapTip>
                                            <Border CornerRadius="10" BorderBrush="#FF51626F" BorderThickness="2" Margin="0,0,15,15" Background="LightGray">
                                                <StackPanel Margin="7">
                                                    <StackPanel Orientation="Vertical">
                                                        <TextBlock Text="{Binding [SCHNAME]}" Foreground="Black" FontWeight="Bold"/>
                                                        <TextBlock Text="(Click point for more information) " Foreground="Black"/>
                                                    </StackPanel>
                                                </StackPanel>
                                            </Border>
                                        </esri:FeatureLayer.MapTip>
                    </esri:FeatureLayer>

                    <esri:FeatureLayer ID="Non-Public Schools" 
                                       MouseEnter="FeatureLayer_MouseEnter"
                                       MouseLeave="FeatureLayer_MouseLeave"
                                       Visible="False"
                                       Url="website address"
                                       OutFields="SCHNAME, ADDRESS, PHONE, URL">
                                        <esri:FeatureLayer.MapTip>
                                            <Border CornerRadius="10" BorderBrush="#FF51626F" BorderThickness="2" Margin="0,0,15,15" Background="LightGray">
                                                <StackPanel Margin="7">
                                                    <StackPanel Orientation="Vertical">
                                                        <TextBlock Text="{Binding [SCHNAME]}" Foreground="Black" FontWeight="Bold"/>
                                                        <TextBlock Text="(Click point for more information) " Foreground="Black"/>
                                                    </StackPanel>
                                                </StackPanel>
                                            </Border>
                                        </esri:FeatureLayer.MapTip>
                    </esri:FeatureLayer>



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


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


Ok, it appears that it is not turning on and off the layer but having the layer listed in the LayerID's for the legend. - Still not sure how to fix or why it is happening. Something about a tiled map service in the legend causes a flicker of the service areas (dynamic service layer) and then it turns off until the user clicks the radio button (but not the default checked radio button)
0 Kudos