I have created a diagram that uses a library browser. I can drag and drop several object from the library to the document. The task that I need is for an event to fire on the creation of a new shape, and also an event when an already created shape is altered/deleted. I am having trouble finding a way to hook into these events. These are object that will be created during the run-time of the program. I saw examples for adding changed value events for programmatically created object, but I need for these event handlers to apply to all object created from the library browser. Any information on this would be greatly appreciated.
Answer:
The NodeInserted and NoderRemoved events of the document event sink service will be fired whenever a node has been inserted or removed from the document regardless of the way in which this was done. You can subscribe for this events with the following code:
document.EventSinkService.NodeInserted += new ChildNodeEventHandler(OnNodeInserted);
document.EventSinkService.NodeRemoved += new ChildNodeEventHandler(OnNodeRemoved);
private void OnNodeInserted(NChildNodeEventArgs args)
{
if (args.Child is NShape)
{
// do something when a shape is added
}
}
Assume that I have something like this
NGroup myShape = args.Child as NGroup;//this is a group that was added to the library
then I go
if (myShape.Name == "group name")
{
MessageBox.Show("message");
}
It only works for the following
NShape myShape = args.Child as NShape;//this is a group that was added to the library
then I go
if (myShape.Name == "group name")
{
MessageBox.Show("message");
}
Does not work if myShape is a group as described above