BCoelho

Hi everyone.

I'm overriding the OnShapeRemoved method in a partial class of a shape.

My goal is to access the properties of the associated model element of the shape but this.ModelElement is null...

How can I do this How can I access the ModelElement of a removed shape

Thank you all!

BCoelho


Re: Visual Studio Extensibility OnShapeRemoved get this.ModelElement

Pim Vendrig

Removed sounds like all actions have been done, such as deleting the underlying ModelElement. There is no method OnShapeRemoving, but there is a method OnDeleting, wich handles stuff before deleting the element.

Try overriding that method, and perhaps the Property ModelElement might still be available.





Re: Visual Studio Extensibility OnShapeRemoved get this.ModelElement

BCoelho

Thank you very much!

OnDeleting is defined in the ModelElement class... I didn't search so hight in the hierarchy.

OnDeleting is called by the model before the element is deleted.






Re: Visual Studio Extensibility OnShapeRemoved get this.ModelElement

Gokhan Altinoren

I had the same problem in a Deleted rule. I make a note of the model in Deleting and use it in Deleted. Crappy but works for me.

[RuleOn(typeof(ModelProperty), FireTime = TimeToFire.TopLevelCommit)]
public sealed class ModelPropertyDeletingRule : DeletingRule
{
public override void ElementDeleting(ElementDeletingEventArgs e)
{
base.ElementDeleting(e);

ModelProperty property = e.ModelElement as ModelProperty;
if (property != null)
{
Transaction transaction = property.Store.TransactionManager.CurrentTransaction.TopLevelTransaction;

if (transaction != null && !transaction.IsSerializing && !transaction.Context.ContextInfo.ContainsKey(property.Name))
// We're adding the model element to the context so property deleted event will use it to fire the ondeleted event
// on the Model class
transaction.Context.ContextInfo.Add(property.Name, property.ModelClass.Model);
}
}
}

[RuleOn(typeof(ModelProperty), FireTime = TimeToFire.TopLevelCommit)]
public sealed class ModelPropertyDeletedRule : DeleteRule
{
public override void ElementDeleted(ElementDeletedEventArgs e)
{
base.ElementDeleted(e);

ModelProperty property = e.ModelElement as ModelProperty;

if (property != null)
{
Transaction transaction = property.Store.TransactionManager.CurrentTransaction.TopLevelTransaction;
if (transaction != null && !transaction.IsSerializing && transaction.Context.ContextInfo.ContainsKey(property.Name))
{
Model model = transaction.Context.ContextInfo[property.Name] as Model;
if (model != null)
{
model.OnModelPropertyDeleted(e);
transaction.Context.ContextInfo.Remove(property.Name);
}
}
}
}
}