1

I tried using the following code to listen to the layers loaded into MapControl. However, it is found that the next event is not triggered when the layer is added to the layer group. How do you listen to the layer group to add and delete layers?

mEvent = pAxMapControl.ActiveView.FocusMap as IActiveViewEvents_Event;
mEvent.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(Event_ItemAdded);
mEvent.ItemDeleted += new IActiveViewEvents_ItemDeletedEventHandler(Event_ItemDeleted);

private void Event_ItemAdded(object Item)
{
    if (Item is IFeatureLayer)
    {
        FeatureLayerAdded(Item as IFeatureLayer);
    }
    else if (Item is IRasterLayer)
    {
         RasterLayerAdded(Item as IRasterLayer);
    }
    else if (Item is ILasDatasetLayer)
    {
         LasLayerAdded(Item as ILasDatasetLayer);
    }
}

1 Answer 1

3

Construct a subclass inherited from GroupLayerClass, and construct a IGroupLayerEvent interface to monitor whether or not to add layers to the layer group.

public delegate void IGroupLayerEvents_ItemAddedEventHandler(object Item);
public delegate void IGroupLayerEvents_ItemDeletedEventHandler(object Item);

public interface IGroupLayerEvents
{
    event IGroupLayerEvents_ItemAddedEventHandler ItemAdded;
    event IGroupLayerEvents_ItemDeletedEventHandler ItemDeleted;
}
public class GroupLayerClassEx:GroupLayerClass,IGroupLayerEvents
{
    public event IGroupLayerEvents_ItemAddedEventHandler ItemAdded;
    public event IGroupLayerEvents_ItemDeletedEventHandler ItemDeleted;

    public override void Add(ILayer layer)
    {
        base.Add(layer);
        if (ItemAdded != null)
        {
            ItemAdded(layer);
        }
    }

    public override void Clear()
    {
        if (ItemDeleted != null)
        {
            ICompositeLayer pCompositeLayer = this as ICompositeLayer;
            for (int i = 0; i < pCompositeLayer.Count; i++)
            {
                ILayer pCurrentLayer = pCompositeLayer.get_Layer(i);
                ItemDeleted(pCurrentLayer);
            }
        }
        base.Clear();
    }

    public override void Delete(ILayer Layer)
    {
        base.Delete(Layer);
        if (ItemDeleted != null)
        {
            ItemDeleted(Layer);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.