如何在滚动面板时将孩子添加到堆栈面板并保持其在堆栈面板下方的grid.row可见?

时间:2019-04-04 07:38:36

标签: uwp-xaml

我有一个带有Stackpanel和一个按钮的UWP网格。我以编程方式将子级添加到堆栈面板。如果子项太多,则外部网格将变为可滚动状态,并且滚动时堆栈面板和按钮将移动。 我希望按钮停留在屏幕底部(始终可见),并且堆栈面板可滚动。

这是我的xaml代码。

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="60"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <StackPanel x:Name="panel" Orientation="Vertical"/>
    </Grid>
    <Button x:Name="ucBottomMenu" Grid.Row="1" Height="60"/>
</Grid>

1 个答案:

答案 0 :(得分:1)

如果以上代码是页面上的所有XAML代码,则按钮控件应始终可见。您只需要使用ScrollViewer控件来包装stackPanel,然后只有stackPanel中的子元素才能滚动。

XAML如下所示:

<Page
x:Class="AppScroll.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppScroll"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="60" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <ScrollViewer>
                <StackPanel x:Name="panel" Orientation="Vertical" />
            </ScrollViewer>
        </Grid>
        <Button x:Name="ucBottomMenu" Grid.Row="1" Height="60" />
    </Grid>
</Grid>

相关问题