Select to view content in your preferred language

Layers CollectionChanged Event

769
3
11-08-2010 07:02 AM
MatthewPilgrim
Regular Contributor
Hi,

The Map.Layers.CollectionChanged event is firing when I set the DrawMode on a Client.Draw object, is this correct?

I simply wanted to catch when new layers were added or removed but this event fires as the user starts drawing buffers.  I looked at the event args and I can't distinguish between this event firing due to legitimate changes in hte number of layers.

Any ideas?

Matt
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
This is correct, the Draw object, creates a new GraphicsLayer on top of your layers. Therefore Layers.CollectionChanged event will fire.  You can probably ignore if the layer added has no ID to distinguish the layer you create and the layer the API creates dynamically.
0 Kudos
MatthewPilgrim
Regular Contributor
Thank you for the response.  I've added this code:

public void Layers_CollectionChanged(Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
      {
          //this event if fired when this code is executed: MyDrawObject.DrawMode = DrawMode.... 
          //so we look for single layers being added/removed with null Ids
          //we supress the refresh in these circumstances

          bool DontFire = false;
         
          if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
          {
              if (e.NewItems != null  && e.NewItems.Count == 1)
              {
                  if (((ESRI.ArcGIS.Client.Layer)e.NewItems[0]).ID == null)
                  {
                      DontFire = true;
                  }
              }
          }
          else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
          {
              if (e.OldItems != null && e.OldItems.Count == 1)
              {
                  if (((ESRI.ArcGIS.Client.Layer)e.OldItems[0]).ID == null)
                  {
                      DontFire = true;
                  }
              }
          }

          if (DontFire == false)
          {
              Refresh();
          }
       }


Hopefully that will do the job.
0 Kudos
JenniferNery
Esri Regular Contributor
Yup I think that should work.

Another suggestion is that you can use
!string.IsNullOrEmpty(layer.ID)
0 Kudos