Is it possible for a WPF control to have an ActualWidth and ActualHeight if it has never been render

Posted by DanM on Stack Overflow See other posts from Stack Overflow or by DanM
Published on 2010-06-01T06:40:30Z Indexed on 2010/06/01 6:43 UTC
Read the original article Hit count: 1252

Filed under:

I need a Viewport3D for the sole purpose of doing geometric calculations using Petzold.Media3D.ViewportInfo. I do now want to place it in a Window.

I'm creating a Viewport3D using the following code:

private Viewport3D CreateViewport(MainSettings settings)
    var cameraPosition = new Point3D(0, 0, settings.CameraHeight);
    var cameraLookDirection = new Vector3D(0, 0, -1);
    var cameraUpDirection = new Vector3D(0, 1, 0);
    var camera = new PerspectiveCamera
        Position = cameraPosition,
        LookDirection = cameraLookDirection,
        UpDirection = cameraUpDirection
    var viewport = new Viewport3D
        Camera = camera,
        Width = settings.ViewportWidth,
        Height = settings.ViewportHeight
    return viewport;

Later, I'm attempting to use this viewport to convert the mouse location to a 3D location using this method:

public Point3D? Point2dToPoint3d(Point point)
    var range = new LineRange();
    var isValid = ViewportInfo.Point2DtoPoint3D(_viewport, point, out range);
    if (isValid)
        return range.PointFromZ(0);
        return null;

Unfortunately, it's not working. I think the reason is that the ActualWidth and ActualHeight of the viewport and both zero (and these are read-only properties, so I can't set them manually). (I have tested the exact same with an actual rendered Viewport3D, so I know the issue is not with my converter method.)

Any idea how I can get WPF to assign the ActualWidth and ActualHeight based on my Width and Height settings?

I tried setting the HorizontalAlignment and VerticalAlignment to Left and Top, respectively, and I also messed with the MinWidth and MinHeight, but none of these properties had any effect on the ActualWidth or ActualHeight.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf