如果 WPF 控件从未呈现过,它是否可能具有 ActualWidth 和 ActualHeight? [英] Is it possible for a WPF control to have an ActualWidth and ActualHeight if it has never been rendered?

查看:22
本文介绍了如果 WPF 控件从未呈现过,它是否可能具有 ActualWidth 和 ActualHeight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 Viewport3D,其唯一目的是使用 Petzold.Media3D.ViewportInfo 进行几何计算.我宁愿不必将它放在 Window 中或以其他方式呈现它.

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.

我试图通过实例化一个 Viewport3D 并使用以下 C# 方法设置一些属性来实现这一点:

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

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

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

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

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.)

知道如何让 WPF 根据 WidthHeight<分配控件的 ActualWidthActualHeight/code> 设置?

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

我尝试将 Horizo​​ntalAlignmentVerticalAlignment 分别设置为 LeftTop,但我也搞砸了MinWidthMinHeight,但这些属性对 ActualWidthActualHeight 没有任何影响.

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.

推荐答案

来自 ActualWidth 属性:

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.

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

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.

这篇关于如果 WPF 控件从未呈现过,它是否可能具有 ActualWidth 和 ActualHeight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆