mosquito87
mosquito87

Reputation: 4440

Visibility="Collapsed" has no effect with GroupBoxes in grids

I have a view with grids and different group boxes. Setting the visibility of all group boxes in the first row to Collapsed should let them disappear (which works) and make "space" for the group boxes below. They should move upwards (which doesn't work):

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

    <Grid Height="Auto" Name="Top">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <GroupBox Grid.Row="0" Grid.Column="0" Header="GroupBox 1" Visibility="Collapsed">
                <Label Content="It (doesn't) work (1)"/>
            </GroupBox>
            <GroupBox Grid.Row="0" Grid.Column="1" Header="GroupBox 2" Visibility="Collapsed">
                <Label Content="It (doesn't) work (2)"/>
            </GroupBox>
            <GroupBox Grid.Row="1" Grid.Column="0" Header="GroupBox 3">
                <Label Content="It (doesn't) work (3)"/>
            </GroupBox>
            <GroupBox Grid.Row="1" Grid.Column="1" Header="GroupBox 4">
                <Label Content="It (doesn't) work (4)"/>
            </GroupBox>
        </Grid>
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <GroupBox Grid.Row="0" Grid.Column="0" Header="GroupBox 5">
                <Label Content="It (doesn't) work (5)"/>
            </GroupBox>
            <GroupBox Grid.Row="0" Grid.Column="1" Header="GroupBox 6">
                <Label Content="It (doesn't) work (6)"/>
            </GroupBox>
            <GroupBox Grid.Row="0" Grid.Column="2" Header="GroupBox 7">
                <Label Content="It (doesn't) work (7)"/>
            </GroupBox>
            <GroupBox Grid.Row="1" Grid.Column="0" Header="GroupBox 8">
                <Label Content="It (doesn't) work (8)"/>
            </GroupBox>
            <GroupBox Grid.Row="1" Grid.Column="1" Header="GroupBox 9">
                <Label Content="It (doesn't) work (9)"/>
            </GroupBox>
            <GroupBox Grid.Row="1" Grid.Column="2" Header="GroupBox 2">
                <Label Content="It (doesn't) work (10)"/>
            </GroupBox>
        </Grid>
    </Grid>
</Window>

So there is a lot of space at the top of my view now. Where's my error?

Upvotes: 1

Views: 722

Answers (2)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174417

The problem is that your rows and columns have fixed heights and widths. None of them is set to Auto.

Upvotes: 0

Domysee
Domysee

Reputation: 12846

You specify your row definitions with Height="1*". This splits up the height evenly across the rows. Collapsing the content of the rows does not affect their height.

If you want your rows to resize according to the content, you should use Height="auto".

Upvotes: 2

Related Questions