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

时间:2014-04-04 09:41:50

标签: 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>

Stretch是否使网格拉伸以容纳其所有内容,或填充其容器?我希望它能够伸展以填充容器Border,并且具有适当的double宽度,我可以使用它将浮动窗口的大小设置为相同的大小。

1 个答案:

答案 0 :(得分:20)

  

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

  

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

相关问题