ScrollViewer在WP7上向后滚动

时间:2012-04-13 08:46:07

标签: windows-phone-7 grid scrollviewer

我创建了一个页面,其中有很多来自用户的输入。因此用户应该能够向下滚动以便能够按上传按钮。要在页面上向下滚动,我在网格外部使用了ScrollViewer。我可以滚动但页面在向下滚动后继续向后滚动。

这是我的代码:

  <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanelButtom" Grid.Row="1" >
            <ScrollViewer >
                <Grid x:Name="ContentPanel" Background="black">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="154" />
                        <ColumnDefinition Width="326" />
                    </Grid.ColumnDefinitions>
                    <Image Height="109" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="167" Margin="23,19,0,0" Source="{Binding Path=ImageSoruce, Mode=TwoWay}" Grid.ColumnSpan="2" />
                    <TextBox Height="71" HorizontalAlignment="Left" Margin="12,161,0,0" Name="nameInput" Text="{Binding Path=Name, Mode=TwoWay}" VerticalAlignment="Top" Width="430" Grid.ColumnSpan="2" />
                    <TextBlock Height="56" HorizontalAlignment="Left" Margin="23,134,0,0" Name="nameLabel" Text="Name" VerticalAlignment="Top" Width="130" FontSize="25" />
                    <TextBlock FontSize="25" Height="60" HorizontalAlignment="Left" Margin="23,238,0,0" Name="descriptionLabel" Text="Description" VerticalAlignment="Top" Width="130" />
                    <TextBox Height="72" HorizontalAlignment="Left" Margin="12,265,0,0" Name="descriptionInput" Text="{Binding Path=Description, Mode=TwoWay}" VerticalAlignment="Top" Width="430" IsEnabled="True" Grid.ColumnSpan="2" />
                    <TextBlock FontSize="25" Height="60" HorizontalAlignment="Left" Margin="23,343,0,0" Name="locationLabel" Text="Location" VerticalAlignment="Top" Width="130" />
                    <TextBlock Height="46" HorizontalAlignment="Left" Margin="24,384,0,0" Name="locationInput" Text="{Binding Path=Location, Mode=TwoWay}" VerticalAlignment="Top" Width="401" Loaded="locationInput_Loaded" Grid.ColumnSpan="2" />
                    <toolkit:ListPicker SelectionMode="Multiple" FullModeHeader="CATEFORIES" x:Name="ListPickerCategories" CacheMode="BitmapCache" HorizontalAlignment="Left" Margin="25,492,0,0" VerticalAlignment="Top" Width="401" Grid.ColumnSpan="2" ItemsSource="{Binding Categories}" Height="78"></toolkit:ListPicker>
                    <TextBlock FontSize="25" Height="60" HorizontalAlignment="Left" Margin="24,436,0,0" Name="textBlock1" Text="Categories" VerticalAlignment="Top" Width="130" />
                    <Button Content="Upload" Height="86" HorizontalAlignment="Left" Margin="26,604,0,0" Name="UploadButton" VerticalAlignment="Top" Width="411" Click="UploadButton_Click" Grid.ColumnSpan="2" />
                </Grid>
            </ScrollViewer>
            </Grid>

导致此问题的原因是什么?如何解决?

2 个答案:

答案 0 :(得分:6)

使用StackPanels将元素放在另一个之下。删除所有marginsheightwidth和其他绝对定位内容

<Grid x:Name="ContentPanelButtom" Grid.Row="1" >
    <ScrollViewer>
        <StackPanel>
            <!-- elements here -->
        </StackPanel>
    </ScrollViewer>
</Grid>

答案 1 :(得分:0)

我有同样的问题。但最后我解决了它,我只是使用了Height属性来做到这一点。请执行以下步骤

  • 首先创建一个ScrollViewer
  • 让ScrollViewer创建一个容器(例如:Grid / StackPanel / Border等...)并将每个控件放入其中。
  • 为ScrollViewer和Container设置固定高度(注意:容器的高度应该大于ScrollViewer的高度)

请参阅以下代码

<ScrollViewer Height="500">
        <Grid Name="Container" Height="700">
            <TextBox/>
            <TextBox/>
            <TextBox/>
        </Grid>
</ScrollViewer>

现在您可以滚动容器Grid甚至显示的KeyBoard,甚至可以将焦点放在TextBox上。