Select to view content in your preferred language

Unusual Legend Behavior for Grouping

2199
7
06-03-2011 04:37 AM
CyndieEnfinger
Deactivated User
I have SP1 installed for ArcGISServer 10 and am using the Legend tool from the toolkit in Tree mode with a template to group the sublayers on an ArcGISDynamicMapServiceLayer.  I'm assigning the LayerIDs to keep the Graphics Layer from being listed in the Legend.  The legend displays correctly and if I check or uncheck a checkbox on a layer that has no sublayers, it works correctly.  If, however, I uncheck on a group layer that has sublayers, the entire map goes blank.  If I recheck the group layer, the entire map comes back.  The behaviour should be to turn off just the group related layers and I'm unsure what is causing this.  I think my configuration is quite basic.   Any help would be appreciated.

XAML
  <esri:Legend x:Name="myLegend" Map="{Binding ElementName=map}" LayerItemsMode="Tree"  ShowOnlyVisibleLayers="False" Refreshed="myLegend_Refreshed">
            <esri:Legend.LayerTemplate>
             <DataTemplate>
              <CheckBox Content="{Binding Label}"
                  IsChecked="{Binding IsEnabled, Mode=TwoWay}"
               IsEnabled="{Binding IsInScaleRange}" >
              </CheckBox>
             </DataTemplate>
            </esri:Legend.LayerTemplate>
        </esri:Legend>


Code Behind C#
        private string[] layerList = { "Utilities" };

  public LegendControl()
  {
   // Required to initialize variables
   InitializeComponent();
            this.Loaded += new RoutedEventHandler(LegendControl_Loaded);
 
  }

        void LegendControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.myLegend.Map = map;
            this.legend.LayerIDs = layerList;
            this.legend.Refresh();
        }

        private void myLegend_Refreshed(object sender, Legend.RefreshedEventArgs e)
        {
            if (e.LayerItem.LayerItems != null)
            {
                foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)
                {
                    if (layerItemVM.IsExpanded)
                        layerItemVM.IsExpanded = false;

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

        }
0 Kudos
7 Replies
DominiqueBroux
Esri Frequent Contributor
It might happen that you are using a cached dynamic map service layer or a tiledlayer which doesn't support toggling on/off the sublayers.
In 2.1, when toggling on/off such a sublayer, the layer itself was toggled on/off.

You should be able to check that by looking at the check box corresponding to the layer itself. After toggling a sublayer, does the layer checkbox toggle as well?

Note : from 2.2 this behaviour has been modified. Toggling a sublayer that doesn't support it   will do nothing.
0 Kudos
CameronKuykendall
Emerging Contributor
I am having a similar issue related to this.  When a group layer is unchecked, all the layers with this group are toggled off, which is correct.  However, the sublayer checkboxes still remain checked.  Is there anyway to correct this behavior?  Also, when everything in the group is unchecked, and the group layer is checked, none of the sublayers checkboxes are checked.
0 Kudos
CyndieEnfinger
Deactivated User
I have the same problem.
0 Kudos
CyndieEnfinger
Deactivated User
The individual layers do not toggle when the group level check box is toggled, but the whole map goes blank.

I'm using a dynamic map service.
0 Kudos
DominiqueBroux
Esri Frequent Contributor

When a group layer is unchecked, all the layers with this group are toggled off, which is correct. However, the sublayer checkboxes still remain checked. Is there anyway to correct this behavior?

That is the normal behavior because your checkbox is likely binded to the 'IsEnabled' property of the LayerItemViewModel (that is the case with the Interactive SDK sample which mimics the ArcMap behavior).

'IsEnabled' represents an on/off status of the layer but this status is not depending on its ascendants. A layer can be 'Enabled' even if its parent is not. It just means that the layer is ready to be visible once its ascendants will be checked on.
To be visible a layer AND all its ascendants must be 'Enabled' (note : IsInScaleRange needed as well).

The LayerItemViewModel 'IsVisible' gives the current visibility of the layer. This property is read-only.

By mixing the use of 'IsEnabled' and 'IsVisible' in your legend template, you should able to get what you want.
For example you could use the 'IsVisible' property to get a grayed effect of the checkbox with this template:
<esri:Legend.LayerTemplate>
    <DataTemplate>
        <Grid>
            <CheckBox Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" />
            <CheckBox Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" IsEnabled="{Binding IsVisible}" />
        </Grid>
    </DataTemplate>
</esri:Legend.LayerTemplate>

Note : the trick is the 2 overlapping checkboxes because the grayed one is not enable and so don't allow to turn on a layer.



Also, when everything in the group is unchecked, and the group layer is checked, none of the sublayers checkboxes are checked.

Still same explanation. A group layer can be checked without having any sublayers checked.

If you want to automatically check all sublayers when the user checks the group layer, you can do it by code.

Hope this help.
0 Kudos
CameronKuykendall
Emerging Contributor

If you want to automatically check all sublayers when the user checks the group layer, you can do it by code.


Can you get me started on this?  I can hook the events for checked and unchecked, but I do not know how to find the parent and/or children checkboxes.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Can you get me started on this? I can hook the events for checked and unchecked, but I do not know how to find the parent and/or children checkboxes.

The LayerItemViewModel is the DataContext. So you can use it to go through the children.
private void SetChildren(object sender, RoutedEventArgs e)
{
  CheckBox checkBox = sender as CheckBox; 
 LayerItemViewModel layerItem = checkBox.DataContext as LayerItemViewModel;
   // Update IsEnabled property of the children
  if (layerItem != null && layerItem.LayerItems != null)
      foreach (var item in layerItem.LayerItems)
          item.IsEnabled = checkBox.IsChecked.Value;
}



And in XAML:
<CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay}" Checked="SetChildren" Unchecked="SetChildren"/>
0 Kudos