Home > Enterprise >  Xamarin iOS - How to get StackLayout to auto size to nested contents?
Xamarin iOS - How to get StackLayout to auto size to nested contents?

Time:10-16

I have the following XAML :

 <StackLayout>
     <Grid IsVisible="{Binding isVisible}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                        <behaviors:Expander x:Name="MainExpander" CollapseAnimationLength="500" IsExpanded="false"  >
                            <behaviors:Expander.Header>
                                <Grid HorizontalOptions="FillAndExpand">                                  
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="100"/>
                                        <ColumnDefinition Width="10"/>
                                    </Grid.ColumnDefinitions>
                                    <Frame HeightRequest="40" WidthRequest="40" CornerRadius="20" HorizontalOptions="Start" VerticalOptions="Center" Margin="20" Padding="0" BackgroundColor="Maroon">
                                        <Label Text="{Binding student_initial}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
                                    </Frame>
                                    <StackLayout Grid.Column="2" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Margin="20">
                                        <Label x:Name="StudentName" Text="{Binding student_fullname}"></Label>
                                        <Label x:Name="StudentID" IsVisible="false" Text="{Binding student_unique_id}"></Label>
                                    </StackLayout>
                                </Grid>
                            </behaviors:Expander.Header>
                            <Grid RowSpacing="0" HorizontalOptions="FillAndExpand" HeightRequest="240" VerticalOptions="FillAndExpand">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Button Grid.Row="0" Text="Messages" Clicked="Button_Clicked"></Button>
                                <Button x:Name="btnTopUp" Grid.Row="1" Text="Quick Topup" Clicked="Button_Clicked" IsVisible="{Binding topup_product_id, Converter={StaticResource IsNotNullOrEmptyConverter}}"></Button>
                                <Button Grid.Row="2" Text="Payments" Clicked="Button_Clicked"></Button>
                            </Grid>
                            <!--TODO: Look at adding a balance for childrens topups?-->
                        </behaviors:Expander>
                    </Grid>
                        </StackLayout>

I am trying to hide and show the grid as follows :

<Grid IsVisible="{Binding isVisible}" ...

The issue is that nothing shows as it looks like the StackLayout is unable to work out how high it needs to be. I can't explicitly set the height property as it depends if the expander is expanded or not. Is there a way to make the stack layout height auto size ? Thank you.

CodePudding user response:

Replace StackLayout with Grid , set its HorizontalOptions and VerticalOptions as Start .

 <Grid BackgroundColor="Red" HorizontalOptions="Start" VerticalOptions="Start">
        <Grid  IsVisible="false" ...

See the following screen shot in all of the scenarios (red color represents the root Grid)

Gird invisible

enter image description here

Grid visible (none-expanded)

enter image description here

Grid visible (expanded)

enter image description here

  • Related