RDV
RDV

Reputation: 1026

WPF Grid with Row heights * not expanding to Grid's max height

My WPF Grid has 3 rows (Auto, *, *) and maxHeight set to 500. When there is no content for second or 3rd row, Grid still doesn't expand to its maxHeight.

XAML Code:

<Grid MaxHeight="500">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!-- Button occupies 30 px-->
    <Button Grid.Row="0" Content="SwitchGrid"/>
    <ContentControl Grid.Row="1" Content="{Binding DG1}"/>
    <ContentControl Grid.Row="2" Content="{Binding DG2}"/>
</Grid>
                                         

Problem Statement:

What can I do to make sure Grid expands to MaxHeight if any of its children need it?

Thanks,

RDV

Upvotes: 0

Views: 441

Answers (1)

Muhammad Sulaiman
Muhammad Sulaiman

Reputation: 2625

You can bind the row height to a string property..

<Grid MaxHeight="500">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="{Binding Row1Height, FallbackValue=*}"/>
        <RowDefinition Height="{Binding Row2Height, FallbackValue=*}"/>
    </Grid.RowDefinitions>

    <!-- Button occupies 30 px-->
    <Button Grid.Row="0" Content="SwitchGrid"/>
    <ContentControl Grid.Row="1" Content="{Binding DG1}"/>
    <ContentControl Grid.Row="2" Content="{Binding DG2}"/>
</Grid>

Then you can update it whenever you want, for example..

public DG1 DG1 {
    set{
        // ..
        Row1Height = value == null ? "0" : "*";
    }
    get{
        // ..
    }
}

Upvotes: 1

Related Questions