如果 WPF 控件从未被渲染过,它是否可能具有 ActualWidth 和 ActualHeight?

发布于 2024-09-03 07:52:51 字数 1637 浏览 2 评论 0原文

我需要一个 Viewport3D ,其唯一目的是使用 Petzold.Media3D.ViewportInfo 进行几何计算。我不想将其放置在 Window 中或以其他方式渲染它。

我尝试通过实例化 Viewport3D 并使用以下 C# 方法设置一些属性来实现此目的:

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;
}

稍后,我尝试使用此视口将鼠标位置转换为 3D 位置方法:

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

不幸的是,它不起作用。我认为原因是视口的ActualWidth和ActualHeight都为零(并且这些是只读属性,因此我无法手动设置它们)。 (注意:我已经使用实际渲染的 Viewport3D 测试了完全相同的方法,并且工作正常,所以我知道问题不在于我的转换器方法。)知道

如何让 WPF 进行分配基于 WidthHeight 设置的控件的 ActualWidthActualHeight

我尝试将 Horizo​​ntalAlignmentVerticalAlignment 分别设置为 LeftTop,而且我也弄乱了 >MinWidthMinHeight,但这些属性对 ActualWidthActualHeight 没有任何影响。

I need a Viewport3D for the sole purpose of doing geometric calculations using Petzold.Media3D.ViewportInfo. I would prefer not to have to place it in a Window or otherwise render it.

I'm attempting to accomplish this by instantiating a Viewport3D and setting a few properties using the following C# method:

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);
    else
        return null;
}

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

Any idea how I can get WPF to assign the ActualWidth and ActualHeight of a control based on the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2024-09-10 07:52:51

来自 ActualWidth 的 MSDN 主题属性:

此属性是基于其他宽度输入和布局系统的计算值。该值由布局系统本身设置,基于实际渲染通道,因此可能稍微落后于作为输入更改基础的属性(例如宽度)的设置值。

所以,这听起来像是要设置属性需要渲染通道。但是,您可以尝试调用 Measure(Size ),然后 Arrange(Rect) 模拟布局过程。也许这已经足够了。

From MSDN topic for the ActualWidth property:

This property is a calculated value based on other width inputs, and the layout system. The value is set by the layout system itself, based on an actual rendering pass, and may therefore lag slightly behind the set value of properties such as Width that are the basis of the input change.

So, this sounds like a rendering pass is necessary for the property to be set. However, you could try to call Measure(Size) and then Arrange(Rect) to simulate the layout process. Maybe this is already sufficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文