2014-04-04 22 views
11

我在视图中有以下标记。当在视图的启动代码中获得WindowContainer.Width时,它将返回NaN为什么我的网格宽度为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是否让网格伸展以容纳其所有内容或填充其容器?我希望它可以伸展以填充容器,即Border,并且具有合适的宽度,我可以使用该宽度来调整浮动窗口的大小。

+5

'Double.NaN'只是'Width'属性的默认值。这意味着没有明确设置宽度。您应该始终使用'ActualWidth'属性来获取元素的实际宽度。 – Clemens

回答

16

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

MSDNHorizontalAlignment="Stretch"

子元素被拉伸,以填补父元素的已分配布局空间。明确的WidthHeight值优先。


Why is my Grid's width NaN?

NaN是指 “未设置”。 FrameworkElement是WPF中许多控件的基类,如果您没有明确设置高度和宽度属性,那么在类构造函数中默认值为NaN


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

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

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

+1

我在网格的OnInitialized事件中得到0.0。我应该在视图启动后期获得价值吗? – ProfK

+1

@ProfK:是的,例如,尝试在'Loaded =“WindowContainer_OnLoaded”'中获取这些值。 –

+0

在这种情况下,我得到了“ActualWidth”的正确值,但ActualHeight仍然返回0.0 – ProfK

相关问题