WPF RowDefinition Weirdness

759 Views Asked by At

I have an app I'm trying to debug, and I'm looking at various settings at runtime via snoop.

The problem I'm having is this:

There is a grid, with a height of 50.4 . It has 3 RowDefinitions each with a height of 1 star (*). Therefore one would assume that each RowDefinition will have an actual height of 50.4/3 (16.8). However, I can see that the first 2 RowDefinitions have an ActualHeight of 25.2 and the third has an ActualHeight of 0. All three still have a Height of 1 star.

How is this possible ? I just can see how you can achieve this situation. You cant change the ActualHeight grammatically as its read-only. The heights must have been set in the Grid's Arrange pass.

Does anyone know of any set of circumstances that would produce this scenario.

1

There are 1 best solutions below

4
On

I've found the answer - I don't know if its a WPF bug, or that I haven't read the documentation properly

<Border Background="Red" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBox Text="123" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBox Text="123" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBox Text="123" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBox Text="123" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBox Text="123" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed" />
        <TextBox Text="123" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed" />
    </Grid>
</Border>

If you look at the code, you will see that there are 3 star(*) sized rows, which should ALL have equal share of the grid's height, regardless of their contents.

However, when you collapse ALL of the contents assigned to that row, the row itself ALSO collapses. (if you snoop it it has a height of *, but an ActualHeight of 0)

Does anyone consider this normal behavior ? Ive never heard of this in all of my WPF development over the last 5 years