19

I have the following mark-up in a view. When I get WindowContainer.Width during start-up code for the view, it returns 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 Stretch make 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 double width I can use to size a floating window to be the same size.

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • 5
    `Double.NaN` is just the default value of the `Width` property. It means that no width is set explicitly. You should always use the `ActualWidth` property for getting the actual width of an element. – Clemens Apr 04 '14 at 09:59

1 Answers1

31

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

From MSDN about 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 is to mean "not set". FrameworkElement is 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.


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

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
  • 1
    I get 0.0 in the grid's `OnInitialized` event. Should I be getting the value later in the view startup? – ProfK Apr 04 '14 at 09:58
  • 1
    @ProfK: Yes, try get these values in `Loaded="WindowContainer_OnLoaded"` for example. – Anatoliy Nikolaev Apr 04 '14 at 10:03
  • I get the correct value for `ActualWidth`, in that event, but `ActualHeight` still returns 0.0 – ProfK Apr 04 '14 at 10:47
  • @ProfK: For me works fine. For `WindowContainer` ActualHeight==Height is 400. – Anatoliy Nikolaev Apr 04 '14 at 10:53
  • Sorry, I left the explicit `Height` property in my code sample. It is now removed in my code and the sample. Shouldn't the grid stretch to fill the border, which surrounds the outside of the control and has height inside it? – ProfK Apr 04 '14 at 11:14
  • @ProfK: When I set the content of `Grid` panel, when ActualHeight is work. If I understand your question, yes. – Anatoliy Nikolaev Apr 04 '14 at 11:21
  • My Grid has no content but an HwndHost derivative that hosts a Win32 Window. I'm trying to resize that window to the same size as the grid and position it over it, so it looks like the grid contains it. When I set grid content, `ActualHeight` is that of the content, not the container. – ProfK Apr 04 '14 at 11:38