Youp Bernoulli
Youp Bernoulli

Reputation: 5645

Automatically resize rows of a grid contained in another grid

I have a grid within the first column of another grid and want the rows of the inner grid take on the same height of the rows of the parent grid. The idea is that the inner grid can contain descriptions that I want to be able to hide (but stay aligned with the rows of the parent grid, because these descriptions are associated with certain questions in the parent grid). A WPF ColumnDefinition doesn't have a Visibility property :(

The code I have thusfar:

    <Grid ShowGridLines="true" Name="gridje">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" Name="row0"/>
        <RowDefinition Height="Auto" Name="row1"/>
        <RowDefinition Height="Auto" Name="row2"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid Grid.Row="0" Grid.RowSpan="3" ShowGridLines="true">
        <Grid.RowDefinitions>
            <RowDefinition Height="{Binding ElementName=row0, Path=ActualHeight, Mode=OneWay}"/>
            <RowDefinition Height="{Binding ElementName=row1, Path=ActualHeight, Mode=OneWay}"/>
            <RowDefinition Height="{Binding ElementName=row2, Path=ActualHeight, Mode=OneWay}"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0">Joepie</TextBlock>
        <TextBlock Grid.Row="2">Joepie</TextBlock>
    </Grid>
    <TextBlock Grid.Row="0" Grid.Column="1" TextWrapping="WrapWithOverflow">Klaasje daskljf askldfas dfasjkl dfhklasjdfh askljdfh askljdfh h askljdfh klasdfh alsjkdfh askldfh askljfh alsdjkfh asklfh </TextBlock>
    <TextBlock Grid.Row="1" Grid.Column="1" TextWrapping="WrapWithOverflow">Klaasje daskljf askldfas dfasjkl dfhklasjdfh askljdfh askljdfh h askljdfh klasdfh alsjkdfh askldfh askljfh alsdjkfh asklfh</TextBlock>
    <TextBlock Name="textb" Grid.Row="2" Grid.Column="1" TextWrapping="WrapWithOverflow" Text="{Binding ElementName=row0, Path=ActualHeight, Mode=OneWay}"></TextBlock>
    <Button Content="Button" Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="66,173,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

The strange thing is that in design mode in VS2010 everything works fine, but once running all the actual height properties stay zero.

Any help on this would be appreciated. And maybe using some other WPF control to accomplish this is also no problem.

Upvotes: 0

Views: 564

Answers (1)

HCL
HCL

Reputation: 36775

Maybe the Grid.IsSharedSizeScope -property will help you. With this, you can synchronize widths and heigths of Grid-elements. An example on how to use it is contained in the msdn-article.

Upvotes: 2

Related Questions