I captured (stopped) a Layer (featureclass) from being deleted from the Table of contents of arcmap. When i re-add it back, the datasource is broken.
(See question :Using ArcObjects to stop items from being deleted from Table of Contents?)
I'm wondering what I need to cast my ILayer object to, in order to just activate the datasource?
The workspace is sde, but i find it odd that if the whole object(layer) is available when caught by the itemdeleted handlers, why is the datasource off?
I'm having trouble finding the right interface to use.
Here is the code that I have implemented: It readds the layer back to the ToC but the connection is not connected.
(Exten
is the Addin.extension object for the addin.)
public void On_TOCItemDeleted(object Item)
{
IWorkspaceName workspace;
var msgText = "You tried to delete an item from the TOC.";
// check that the item is of type ILayer (lyr file, raster, etc) before doing anything.
var DeletedLayer = Item as ILayer;
if (DeletedLayer == null)
throw new Exception("Not iLayer file");
IDataLayer2 src = DeletedLayer as IDataLayer2;
var groupname = FindParentGroup(DeletedLayer.Name);
if (src.DataSourceName != null)
{
workspace = (src.DataSourceName as IDatasetName).WorkspaceName;
}
else
{
switch (groupname)
{
case BLayerName: // basemaps etc
workspace = DeletedLayer.SetLayerSDEConnection(Exten.Analysis_WIP);
break;
default: //analysis connection
workspace = DeletedLayer.SetAnalysisSDEConnection(Exten.Analysis_WIP);
break;
}
}
src.Connect((workspace as IName));
msgText = string.Format("You tried to delete the layer '{0}'. This operation was stopped.", DeletedLayer.Name);
//Read the item
var grouplyr = GetGroupByName(ArcMap.Document.FocusMap.Layers, groupname);
grouplyr.Add(src as ILayer);
ArcMap.Document.UpdateContents();
ArcMap.Document.ActiveView.Refresh();
Exten.InformUser("App", msgText);
}
IDatalayer2
. Maybe the DataSourceName is still populated, and you can just re-connect it. Typically this points to anIFeatureclassName
for a featurelayer.