Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is there any other event besides IMapEvents.FeatureClassChanged that will be fired when a layer's data source changes? FeatureClassChanged doesn't seem to fire if the layer's previous data source was invalid (red exclamation mark next to the layer in the TOC).

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Well I guess the only answer to this question is that the only event that is fired when a layer's data source changes from a broken data source is IActiveViewEvents.ContentsChanged.

It doesn't tell you what changed, only that something changed, which is all I really needed. I suppose if you need to know what changed you would have to cache the state of what you were looking for and check it each time this event is fired.

share|improve this answer

Well, I was going to test this using Esri's sample code from here.

However, it doesn't fire for me even when the featureclasses are not broken.

private void Test()
{
    var fLayer1 = ArcMap.Document.FocusMap.get_Layer(0) as IFeatureLayer;
    var fLayer2 = ArcMap.Document.FocusMap.get_Layer(1) as IFeatureLayer;
    if (dFeatClsChangedE == null)
    {
        ListenToMapEvents(ArcMap.Application);
    }
    // swap the featureclasses
    var fc1 = fLayer2.FeatureClass;
    var fc2 = fLayer1.FeatureClass;

    fLayer1.FeatureClass = fc1;
    fLayer2.FeatureClass = fc2;
    fLayer1.Name = fLayer1.FeatureClass.AliasName;
    fLayer2.Name = fLayer2.FeatureClass.AliasName;
    ArcMap.Document.CurrentContentsView.Refresh(null);
}
private IMapEvents_FeatureClassChangedEventHandler dFeatClsChangedE;
private void ListenToMapEvents(IApplication application)
{
    IDocument document = application.Document;
    IMapDocument mapDoc = document as IMapDocument;
    IActiveView activeView = mapDoc.ActiveView;
    IMap map = activeView.FocusMap;

    IMapEvents_Event mapEvents = map as IMapEvents_Event;

    //Initialize the delegate to point to a function where you respond to the event being raised.
    dFeatClsChangedE = new IMapEvents_FeatureClassChangedEventHandler
        (OnFeatureClassChangedFunction);

    // Wire the delegate to the FeatureClassChanged event of the mapEvents variable.
    mapEvents.FeatureClassChanged += dFeatClsChangedE;
}

private void OnFeatureClassChangedFunction(IFeatureClass oldClass,
    IFeatureClass newClass)
{
    // Listen to the FeatureClassChanged event of IMapEvents.
    MessageBox.Show("Feature Class changed");
}
share|improve this answer
    
FeatureClassChanged works for me when I change the feature layer's data source manually (and the original data source was valid), but it doesn't surprise me that it doesn't work when the data source is changed programatically. –  blah238 Sep 1 '11 at 22:08
1  
Yeah, I guess I've just gotten spoiled with WPF/Silverlight's INotifyPropertyChanged. If you agree, please vote for my idea. –  Kirk Kuykendall Sep 1 '11 at 22:47
    
Great idea -- I would definitely vote for it if I could log in, something is broken though (what else is new :p) –  blah238 Sep 1 '11 at 22:55
    
I went through all the "Events" interfaces in the SDK and there is nothing else that I saw that would work here. The only alternative I can see is to implement your own TOC or track every layer addition and removal and poll them all periodically and note when the Valid property changes. Yuck. The whole reason I went with events was so I wouldn't have a heavy duty method in my add-in components' OnUpdate method. –  blah238 Sep 1 '11 at 22:59
1  
Actually it looks like IActiveViewEvents.ContentsChanged will be good enough for my purposes. It doesn't tell you what changed, only that something changed, which is all I really need. –  blah238 Sep 1 '11 at 23:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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