Select to view content in your preferred language

Activate a Layer using Layer Template/TOC with Radio Button

401
0
03-28-2013 05:45 AM
KarenRobine
Frequent Contributor
Hello:

I'm using the Legend Template to build in the concept to activate a layer. I've setup a Radio button for all of the layers that appear in the Legend Template (you'll see 2 radio buttons in the code below). When the user clicks on the radio button of that layer, I want to Activate that layer. The layer may either be a layer within a Dynamic Map Service layer, an actual Dynamic Map Service layer, or a Tiled map Service layer. I'm having 2 problems with this concept:

1. When I first open the Legend Template, no radio buttons are checked. I want to control which radio button is checked (I want one of my Dynamic Map Service layers radio buttons to be checked. I've created some Loaded events for both of my radio buttons but I can't set the current radio button to IsChecked when I get to the one I'd like to check.

2. I've setup an event to handle the Click Event. It's working nicely for my Dynamic Map Service and Tiled Map Service layers. But it's not working at all for my Dynamic Map Service Layer sublayers. I think I havent setup my Tag Properly.

Code Below (sorry, the formatting got messed up when I pulled it in):

  <esri:Legend Name="MyTOC" Map="{Binding ElementName=Map}"
LayerItemsMode="Tree"
ShowOnlyVisibleLayers="False"
Refreshed="TOCLegend_Refreshed">
<esri:Legend.MapLayerTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsEnabled="{Binding IsInScaleRange}" >
</CheckBox>
<RadioButton GroupName="grpLayer" Name="rdMSActiveLayer" IsEnabled="True"
Content="{Binding Label}"
Tag="{Binding Layer.ID}"
Click="rdActiveLayerMS_Click" Loaded="rdMSActiveLayer_Loaded">
</RadioButton>
<Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="50" />
</StackPanel>
</DataTemplate>
</esri:Legend.MapLayerTemplate>
<esri:Legend.LayerTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" x:Name="stackpanel1">
<CheckBox
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsEnabled="{Binding IsInScaleRange}" >
</CheckBox>
<RadioButton GroupName="grpLayer" Name="rdActiveLayer" IsEnabled="True"
Content="{Binding Label}"
Tag="{Binding Layer.ID}"
Click="rdActiveLayer_Click" Loaded="rdActiveLayer_Loaded">
</RadioButton>
</StackPanel>
</DataTemplate>
</esri:Legend.LayerTemplate>
<esri:Legend.LegendItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" x:Name="stackpanel1">
<Image Source="{Binding ImageSource}"></Image>
<sdk:Label Content="{Binding Label}" MinWidth="40"></sdk:Label>
</StackPanel>
</DataTemplate>
</esri:Legend.LegendItemTemplate>
</esri:Legend>


And here's my Loaded method:

private void rdMSActiveLayer_Loaded(object sender, RoutedEventArgs e)
{
RadioButton rb = sender as RadioButton;
object dc = rb.DataContext; //this is null
LayerItemViewModel vm = dc as LayerItemViewModel;
Layer lyr = vm.Layer; //this points to the Dynamic Service Layer (not the sublayer)
//rb.Name = lyr.ID;
if (TOCDialog._ActiveLayer == null)
{
if (lyr != null && (lyr is ArcGISDynamicMapServiceLayer || lyr is ArcGISTiledMapServiceLayer))
{
TOCDialog._ActiveLayer = lyr;
rb.IsChecked = true; //this is not setting for some reason
}
}
}

Thanks for the help.

Karen
0 Kudos
0 Replies