为什么我的网格宽度为NaN?

Pro*_*ofK 16 wpf wpf-controls wpf-4.0

我在视图中有以下标记.当我WindowContainer.Width在视图的启动代码中获取时,它返回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>
Run Code Online (Sandbox Code Playgroud)

是否Stretch使网格伸展以容纳其所有内容,或填充其容器?我希望它伸展以填充容器,Border并且具有适当的double宽度,我可以使用它来调整浮动窗口的大小.

Ana*_*aev 22

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

MSDN有关HorizontalAlignment="Stretch":

子元素被拉伸以填充父元素的已分配布局空间.显式WidthHeight值优先.


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,因为:

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