wpf controls – Wpf Grid rows height is not calculated correctly with SizeToContent – Education Career Blog

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"
    Title="MainWindow" SizeToContent="Height">
<Grid x:Name="grid">
        <RowDefinition x:Name="row0" Height="6*"/>
        <RowDefinition x:Name="row1" Height="4*"/>
    <TextBox Grid.Row="0" x:Name="textBox" TextWrapping="Wrap" 
             HorizontalScrollBarVisibility="Auto" IsReadOnly="True" xml:space="preserve">
    <TextBlock Background="Red" Grid.Row="1"/>


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;


Give the Grid a value for Height or MinHeight:

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

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

Leave a Comment