Select to view content in your preferred language

Map.Layers.CollectionChanged event wont fire on layer removal

982
4
03-23-2012 12:08 PM
JudyOgg
Emerging Contributor
The Map.Layers.CollectionChanged event  is firing just fine when layers are added to the Map, but not when they are removed. It should.
Has anyone else seen this behaviour (or lack of it)?
0 Kudos
4 Replies
JenniferNery
Esri Regular Contributor
What version of the API are you using? I could not reproduce with the following code. I have a button click to remove layer from my map.
            MyMap.Layers.CollectionChanged += (a, b) =>
                {
//breakpoint here
                };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MyMap.Layers.RemoveAt(MyMap.Layers.Count -1);
        }
0 Kudos
JudyOgg
Emerging Contributor
I'm using version 2.4 of the API, 1.0 of the Viewer, 2.4 of the Extensibility SDK.

Here is my code:

In the ctor of my class:
map.Layers.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Layers_CollectionChanged);


The handler:

void Layers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
                   //some code here            
        }
0 Kudos
DominiqueBroux
Esri Frequent Contributor
It might happen that the layers collection changes after you hook up your handler.
In this case you can suscribe to PropertyChanged event of the map, and hook up your handler if the "Layers" property changed.

Not sure it's your case though, not my 2cts.
0 Kudos
JudyOgg
Emerging Contributor
Thanks! What I ended up hooking up to was the PropertyChanged event of the layers themselves, once I realized that the layers werent always being removed, were just having their visibility set to false.
0 Kudos