Reputation: 1433
In a WPF project I have a Grid that has 3 rows. In each row I have a StackPanel containing a TextBlock.
When all the StackPanels are Visible I would like each StackPanel to occupy 1/3 of the Height of the Grid each. But when a single StackPanel is Collapsed then the remaining 2 StackPanels occupy 1/2 of the Height of the Grid each. And, if 2 are collapsed then the remaining one fills the whole Grid.
Below is some sample code illustrating the UI.
If I change the 3rd RowDefinition
Height
to "Auto"
and set the panel3.Visibility = "Collapsed"
then the right thing happens. But then if I change it back to 'Visible' then it doesn't work.
I can't see a way to achieve what I want.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel x:Name="panel1" Grid.Row="0" Visibility="Visible">
<TextBlock x:Name="txt1" Text="FIRST ROW" />
</StackPanel>
<StackPanel x:Name="panel2" Grid.Row="1" Visibility="Visible">
<TextBlock x:Name="txt2" Text="SECOND ROW" />
</StackPanel>
<StackPanel x:Name="panel3" Grid.Row="2" Visibility="Visible">
<TextBlock x:Name="txt3" Text="THIRD ROW" />
</StackPanel>
</Grid>
Upvotes: 1
Views: 59
Reputation: 35646
try UniformGrid with one column instead of Grid:
<UniformGrid Columns="1">
<StackPanel x:Name="panel1" Visibility="Visible">
<TextBlock x:Name="txt1" Text="FIRST ROW" />
</StackPanel>
<StackPanel x:Name="panel2" Visibility="Visible">
<TextBlock x:Name="txt2" Text="SECOND ROW" />
</StackPanel>
<StackPanel x:Name="panel3" Visibility="Visible">
<TextBlock x:Name="txt3" Text="THIRD ROW" />
</StackPanel>
</UniformGrid>
Upvotes: 2
Reputation: 349
When the Visibility
of the Grid
to Collapsed
you can set the height of Grid.RowDefinition
to Auto
like following:
row1.Height = GridLength.Auto;
When the Visibility
of the Grid
to Visible
you can set the height of Grid.RowDefinition
to 1*
like following:
row1.Height = new GridLength(1, GridUnitType.Star);
Upvotes: 0