gpt4 book ai didi

wpf - 为什么我的 Grid 的宽度是 NaN?

转载 作者:行者123 更新时间:2023-12-03 14:00:27 25 4
gpt4 key购买 nike

我在 View 中有以下标记。当我得到 WindowContainer.Width在 View 的启动代码期间,它返回 NaN .

<Border BorderThickness="10">
<StackPanel x:Name="Panel" Orientation="Vertical" >
<Grid x:Name="WindowContainer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Loaded="WindowContainer_OnLoaded">
<delphi:Win32WindowHost x:Name="Host" />
</Grid>
<TextBlock x:Name="InfoTextBlock" HorizontalAlignment="Right" />
</StackPanel>
</Border>

是否 Stretch使网格拉伸(stretch)以容纳其所有内容,还是填充其容器?我希望它拉伸(stretch)以填充容器, Border , 并有一个合适的 double宽度我可以用来将 float 窗口的大小调整为相同的大小。

最佳答案

Does Stretch make the grid stretch to accomodate all its content, or to fill its container?



来自 MSDN 关于 HorizontalAlignment="Stretch" :

Child elements are stretched to fill the parent element's allocated layout space. Explicit Width and Height values take precedence.



Why is my Grid's width NaN?


NaN是“未设置”的意思。 FrameworkElement是 WPF 中许多控件的基类,如果您没有显式设置 Height 和 Width 属性,那么类构造函数中的默认值将是 NaN .

When I get WindowContainer.Width during start-up code for the view, it returns NaN



在这种情况下,尝试获取 ActualWidth , 而不是 Width , 因为:

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

关于wpf - 为什么我的 Grid 的宽度是 NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22859138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com