C# – WPF Bind RowDefinition.Height property to another RowDefinition with Height=Auto within DataTemplate

cwpfxaml

I'd like to make 3 rows in a grid in a WPF datatemplate. The first is set to Height=Auto, the second fills the available space and the third is equal to the first. I've tried with binding to elementname, but that doesn't seem to work

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" x:Name="definition" />
        <RowDefinition Height="*" />
        <RowDefinition Height="{Binding ElementName=definition, Path=ActualHeight}" />
    </Grid.RowDefinitions>

    <TextBox Grid.Row="0" Height="100" />
</Grid>

In this example I hope the height of the third row is also 100px. Any suggestions?

Best Answer

Try this:

<Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid Height="100" Background="Red" x:Name="definition"/>
    <Grid Background="Green" Grid.Row="1"/>
    <Grid Background="Blue" Grid.Row="2" Height="{Binding ElementName=definition, Path=ActualHeight}"/>
</Grid>
Related Topic