locked
PathGeometry.Bounds Not Calculated RRS feed

  • Question

  • I attempt to access the Bounds property of a PathGeometry in my code, but it always returns {0,0,0,0}. I have confirmed that the PathSegments have been successfully added to the PathFigures, and that the PathFigures have been successfully added to the PathGeometry. Why is Bounds always returning {0,0,0,0} instead of the correct values? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, February 26, 2015 8:45 PM

All replies

  • Is your PathGeometry laid out in the visual tree? The bounds won't be set until then.

    If you need more help please provide minimal sample code so we can see what you're doing rather than guessing.

    Friday, February 27, 2015 2:13 AM
    Moderator
  • OK, that sounds like it is probably the reason. However, I am not sure how to get it laid out in the Visual Tree. The method in which I use it is not part of my MainPage class, so I cannot temporarily add it as an element. But because it shouldn't matter what renders the Path or where it is rendered (the bounds will be the same), is there a way for me to lay it out in the Visual Tree without creating an extra page? What I am using this for is to calculate the bounds of only several figures, what I will be putting in the Path in MainPage may be different. Is there a way to calculate bounds in a standalone or external class? Here is the code for my current method:
    Public ReadOnly Property Bounds() As Rect
    	Get
    		Dim pg As New PathGeometry()
    		pg.AddText(Me.Text, Me.CapitalHeight, Me.UpperLeft, Me.Spacing)
    		Return pg.Bounds
    	End Get
    End Property
    (NOTE: AddText(…) is an extension method that adds PathFigures to the PathGeometry)

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, February 27, 2015 3:10 AM
  • Try calling the UIElement's UpdateLayout method to force a layout.
    Tuesday, March 3, 2015 12:21 AM
    Moderator
  • Where would I put the UpdateLayout? As you can see, everything is done inside the Bounds property in the code I posted, so it is never actually part of a UIElement, Bounds is part of a standalone class (it is not part of a Page or UserControl) that is separate from my pages & controls. Any ideas? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, March 3, 2015 3:14 AM