Select to view content in your preferred language

How to set the checkbox checked when the layer is visible?

3243
4
06-06-2011 08:25 AM
DanDong
Deactivated User
Hi guys,

I use a listbox to display sublayers and the Itemtemplate of the listbox is checkbox. I want to implement such function that when some actions, like selection or query task, are done, a few layers are set visible and at the same time the checkboxes are checked. Sorry about my confused description....I mean these checkboxes are not checked by clicking, instead they will be checked by codes.

<Border x:Name="AdministrativeTabBorder" Margin="5,0,5,0" Visibility="Collapsed" Grid.Column="0" Grid.Row="4" BorderBrush="#FFFFFFCC">
                <ListBox x:Name="AdministrativeListBox" FontSize="12" Background="#FFFFFFCC" BorderBrush="#FFFFFFCC">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Margin="2" FontSize="9" 
                                        Name="AdministrativeCheckBox"
                                        Content="{Binding Name}"
                                        IsChecked="{Binding DefaultVisibility, Mode=TwoWay}"
                                        Tag="{Binding ID}"
                                        ClickMode="Press"
                                        Click="AdministrativeCheckBox_Click"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Border>


Here are the actions that layer 20 and 21 are set visible. I want to add some codes here to make checkboxes checked. Any ideas or thoughts? Thank  you! 🙂
dynamicServiceLayer.SetLayerVisibility(21, true);  
dynamicServiceLayer.SetLayerVisibility(20, true);  
0 Kudos
4 Replies
DominiqueBroux
Esri Frequent Contributor
The simplest way to achieve your goal is using the new legend control which is designed for that.

You can retemplate the legend control to display only the sublayers without any swatches.
Instead of your listbox, use something like:

 
<esri:Legend Map="{Binding ElementName=MyMap}" ShowOnlyVisibleLayers="False" LayerItemsMode="Flat"
                LayerIDs="MyDynamicLayerID">
    <esri:Legend.Template>
        <ControlTemplate TargetType="esri:Legend">
            <ListBox ItemsSource="{TemplateBinding LayerItemsSource}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <ToolTipService.ToolTip>
                                <StackPanel MaxWidth="400">
                                    <TextBlock FontWeight="Bold" Text="{Binding Layer.ID}" TextWrapping="Wrap" />
                                    <TextBlock FontWeight="Bold" Text="{Binding Label}" TextWrapping="Wrap" />
                                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                                    <TextBlock Text="{Binding SubLayerID, StringFormat='SubLayer ID : {0}'}" />
                                    <TextBlock Text="{Binding MinimumResolution, StringFormat='Minimum Resolution : {0:F6}'}" />
                                    <TextBlock Text="{Binding MaximumResolution, StringFormat='Maximum Resolution : {0:F6}'}" />
                                </StackPanel>
                            </ToolTipService.ToolTip>
                            <CheckBox Content="{Binding Label}"
                            IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                            VerticalAlignment="Center">
                            </CheckBox>
                            <CheckBox Content="{Binding Label}"
                            IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                            IsEnabled="{Binding IsInScaleRange}" VerticalAlignment="Center">
                            </CheckBox>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </ControlTemplate>
    </esri:Legend.Template>
</esri:Legend>


And you don't need any code:)

Note : don't forget to adapt MyMap and MyDynamicLayerID to your context.
0 Kudos
DanDong
Deactivated User
Thank you Dominique! That works like a charm! 😄 It is perfectly what I want!

I still have a little question here. I adapted the template in my project. In my project, there are two layer tabs, one includes the resources layer (like wetland, soil or lake) and the other includes boundary and infrastructure layers (like road, freeway, county, town). All of the layers are from one single map service (ID = "Visible Layers"), but they are divided into two groups. So how should I specify the sublayerD in this template to group them, or in code behind?

I think in my case two legend controls are needed. One is displaying sublayerID 0-15 and the other is displaying sublayerID 16-24.

<esri:Legend Map="{Binding ElementName=MyMap}" ShowOnlyVisibleLayers="False" LayerItemsMode="Flat" 
LayerIDs="Visible Layers" ScrollViewer.VerticalScrollBarVisibility="Visible">
0 Kudos
DanDong
Deactivated User
Please please help me 🙂
0 Kudos
DanDong
Deactivated User
I think I figured out how to do it. First I copied the layeritem to a LayerItemViewMode array and then cleared the layeritem. In the end I assigned specific layers to the layeritem to form a new layeritem. It sounds like cloning---clearing the original one---pick some from the cloning one and form a new one...

If there is any better idea, I would like to learn from it. Thank you 🙂

void Legend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e)
        {
            ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel[] layerIt = new ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel[34];
            e.LayerItem.LayerItems.CopyTo(layerIt, 0);
            e.LayerItem.LayerItems.Clear();
            int[] layer_num = new int[20] { 15, 14, 7, 16, 18, 19, 31, 29, 30, 33, 23, 21, 20, 17, 10, 5, 32, 26, 7, 15 };
            for (int i = 0; i < 20; i++)
                e.LayerItem.LayerItems.Add(layerIt.ElementAt(layer_num));         
        }
0 Kudos