首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让StackLayout自动调整到嵌套内容的大小?

如何让StackLayout自动调整到嵌套内容的大小?
EN

Stack Overflow用户
提问于 2021-10-14 11:28:55
回答 1查看 77关注 0票数 1

我有以下XAML:

代码语言:javascript
运行
复制
 <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>

我试图隐藏和显示网格,如下所示:

代码语言:javascript
运行
复制
<Grid IsVisible="{Binding isVisible}" ...

问题是什么都没有显示,因为看起来StackLayout无法计算出它需要多高。我不能显式地设置height属性,因为它取决于扩展器是否被展开。有没有办法让堆栈布局高度自动调整大小?谢谢。

EN

Stack Overflow用户

回答已采纳

发布于 2021-10-15 06:22:13

StackLayout替换为Grid,将其HorizontalOptionsVerticalOptions设置为Start

代码语言:javascript
运行
复制
 <Grid BackgroundColor="Red" HorizontalOptions="Start" VerticalOptions="Start">
        <Grid  IsVisible="false" ...

在所有场景中查看下面的屏幕截图(红色表示根网格)

网格不可见

网格可见(无-展开)

网格可见(展开)

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69569916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档