That helped a lot!
However, I think you meant for the code snippet to look like this:
sender, RoutedEventArgs e)
Rect CalculateContentBounds(Rect newShapeBounds)
if(!hasLoaded) will get you the initial bounds calculation which will then be returned via ContentBounds in subsequent calls.
This got me really close, but there are still a few gotcha's.
My diagram data is loaded via an IGraphSource implementation. I set the position and rotation angle of an item before adding it to the graph. Using the above approach results in the initial content bounds calculation being done with the item at the give rotation angle.
This is a problem because I need the initial calculation done when the item is not rotated, and then have the rotation applied after the bounds are calculated.
To get this working I needed to:
- Save of the item's rotation angle
- Set the item's rotation angle to zero
- Add the item to the graph
- Have the diagram draw the item
- Apply the rotation angle
Steps 1-4 are straightforward, but Step 5 is a little tricky because of the timing of applying the rotation angle. I ended up using Dispatcher.BeginInvoke with a low priority to ensure the code inside the begin invoke was called after the diagram had drawn the item initially.
I also had an issue where the item would "walk" on the graph because the position where it ended up after being rotated was not where it was initially saved. I solved this issue in essentially the same way as the rotation issue -- applying the saved position coordinates after the initial render.
It looked something like this:
myContainer.RotationAngle = savedRotationAngle;
The side effect of doing all this after the initial draw is detectable movement of items on load as everything gets re-positioned. Is there a better way of doing this that will eliminate the item jumpiness on load?