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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22859138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:28:05  来源:igfitidea点击:

Why is my Grid's width NaN?

wpfwpf-controlswpf-4.0

提问by ProfK

I have the following mark-up in a view. When I get WindowContainer.Widthduring start-up code for the view, it returns NaN.

我在视图中有以下标记。当我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>

Does Stretchmake the grid stretch to accomodate all its content, or to fill its container? I want it to stretch to fill the container, the Border, and have a proper doublewidth I can use to size a floating window to be the same size.

Stretch让网格拉伸以容纳其所有内容,还是填充其容器?我希望它拉伸以填充容器,Border并且具有适当的double宽度,我可以使用它来将浮动窗口的大小调整为相同的大小。

回答by Anatoliy Nikolaev

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

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

From MSDNabout HorizontalAlignment="Stretch":

MSDN大约HorizontalAlignment="Stretch"

Child elements are stretched to fill the parent element's allocated layout space. Explicit Widthand Heightvalues take precedence.

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



Why is my Grid's width NaN?

Why is my Grid's width NaN?

NaNis to mean "not set". FrameworkElementis the base class for many Controls in WPF and if you do not explicitly set the Height and Width properties then in the class constructor will be a default value of NaN.

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



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

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

In this case try get the ActualWidth, instead of Width, because:

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

ActualWidthproperty is a calculatedvalue 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.

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