No GoObject, nor any GoDocument, should have a reference to any GoView.
I suggest you create your own GoGrid and initialize it the way you want (you could copy the GoView.Grid, perhaps), create a background layer in your GoDocument, and add it to that new layer. You'll want that new background layer to have AllowSelect set to false.
That way your document objects will always have access to the grid.
It's probably pointless to call the base.PositionMidLabel method when you are able to find the grid and call a method on it. But it's a good idea if you're not able to find the grid.
The override is necessary; I was just saying that calling the base method isn’t useful when you’re about to reposition the label anyway. But it doesn’t do any harm.
No, it's just that if you add a reference to a GoView or GoSelection or anything like that in a GoObject or in a GoDocument, it's not going to be serializable, so you won't be able to do a copy-and-paste of the object or of the document. I suppose you could attribute it [NonSerialized] to get around that problem, but it's still against the philosophy of having the document and its objects not be dependent on any views.
Well, the idea of the override was to let the base method figure out the approximate position based on the stroke, and then just ‘nudge’ it onto the grid.
Could you explain how to make the grid available to all objects ?
Does it mean I have to move the existing grid into the document instead of the view ?
Or would it be possible to add the LinksLayer to the view ?
I have a similar issue to Willem. When I drop my GoGeneralNode onto the document from a TreeView, it is not aligned to the grid. It first must be manually Moved. I tried to correct the problem by overriding the OnLayerChanged method, but the view (and thus the grid) was not available.
When something is being dropped from a different kind of Control (i.e. not a GoView such as a GoPalette), GoDiagram doesn’t know anything about what you want to create and add to the GoView.Document. That’s why you need to override GoView.DoExternalDrop (and perhaps other methods, such as GoView.GetExternalDragImage) when handling a drop from an unknown kind of Control.
I assume you do have an override of GoView.DoExternalDrop, where you look at the drag-and-drop data and figure out what to add to the GoDocument. It's at that time that you need to align to the grid, if that's what you want to do. Obviously since you are overriding a GoView method, you will have access to the GoView and its GoView.Grid.