views:

26

answers:

2

In the following example, there are two Grid rows with height of 6* and 4*. The problem is that only after the user changes the the size of the window, the correct height of the rows is calculated. This only happens when the SizeToContent flag is set.
Any ideas why? How can I force the window to calculate the height automatically when it is loaded?

<Window x:Class="TestGridRow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" SizeToContent="Height">
<Grid x:Name="grid">
    <Grid.RowDefinitions>
        <RowDefinition x:Name="row0" Height="6*"/>
        <RowDefinition x:Name="row1" Height="4*"/>
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" x:Name="textBox" TextWrapping="Wrap" 
             HorizontalScrollBarVisibility="Auto" IsReadOnly="True" xml:space="preserve">
        Hallo
        Hallo
        Hallo
        </TextBox>
    <TextBlock Background="Red" Grid.Row="1"/>
</Grid>

A: 

Give the Grid a value for Height or MinHeight:

<Grid x:Name="grid" MinHeight="100">
  ...
</Grid>

This is not a bug: you set the rows to be 60%/40% of the height of the window, but also set the window to size itself to its children(SizeToContent="Height").

Andy
A: 

You should probably just set the SizeToContent to Manual in the Window.Loaded event. This will allow the framework to determine the correct size of the window, and afterwards force the rows to calculate the height correctly.

private void Window_Loaded ( object sender , RoutedEventArgs e )
{
   SizeToContent = SizeToContent.Manual;
}
Ronit