What is the preferred and/or most efficient method for detecting a change in the table of contents in ArcMap using ArcObjects? My current implementation relies on a dictionary with the names and indices of the layers.
public static Dictionary<string, int> layerEnumerator()
{
IEnumLayer enumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
enumLayer.Reset();
ILayer layer = enumLayer.Next();
Dictionary<string, int> layerDictionary = new Dictionary<string, int>();
int lyrNdx = 0;
while (!(layer == null))
{
if (!(layerDictionary.ContainsKey(layer.Name)))
{
layerDictionary.Add(layer.Name, lyrNdx);
}
lyrNdx++;
layer = enumLayer.Next();
}
return layerDictionary;
}
Generally I will call this when I need to find the position of a specific layer in the ToC. Is there a more efficient means of gathering what's in the ToC at any given time (such as an event for ToC Contents Changed)? The solution I've provided above seems inadequate and inefficient at best.