如果 WPF 控件从未被渲染过,它是否可能具有 ActualWidth 和 ActualHeight?
我需要一个 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 进行分配基于 Width
和 Height
设置的控件的 ActualWidth
和 ActualHeight
?
我尝试将 HorizontalAlignment
和 VerticalAlignment
分别设置为 Left
和 Top
,而且我也弄乱了 >MinWidth
和 MinHeight
,但这些属性对 ActualWidth
或 ActualHeight
没有任何影响。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自
ActualWidth
的 MSDN 主题属性:所以,这听起来像是要设置属性需要渲染通道。但是,您可以尝试调用
Measure(Size )
,然后Arrange(Rect)
模拟布局过程。也许这已经足够了。From MSDN topic for the
ActualWidth
property:So, this sounds like a rendering pass is necessary for the property to be set. However, you could try to call
Measure(Size)
and thenArrange(Rect)
to simulate the layout process. Maybe this is already sufficient.