GridView可以垂直滚动而不包装ScrollViewer吗?

时间:2012-12-15 19:30:10

标签: c# xaml windows-8

我有一个GridView绑定到我的C#中的数据源,当用户水平滚动时,它会加载更多数据:

来自GridView.xaml:

<GridView Grid.Row="1" x:Name="GridViewStories" ItemsPanel="{StaticResource GridViewVerticalTemplate}" IsSwipeEnabled="False" SelectionMode="None" IsItemClickEnabled="True" ItemClick="GridViewStories_ItemClick" IncrementalLoadingTrigger="Edge">
    <GridView.ItemContainerTransitions>
        <TransitionCollection />
    </GridView.ItemContainerTransitions>
    <GridView.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding SquareImageURL}" Margin="10" Width="240" Height="160" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

我希望它在捕捉视图时的行为类似于ListView,这意味着它应该垂直滚动而不是水平滚动。除了这个问题,我已经能够通过VisualStateManager改变我需要的一切:

来自GridView.xaml:

<VisualState x:Name="Snapped">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="StackPanelHeader" Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="StackPanelHeaderSnapped" Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="StackPanelHeaderPortrait" Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="GridViewStories" Storyboard.TargetProperty="ItemsPanel">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource GridViewHorizontalTemplate}"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

来自StandardStyles.xaml:

<ItemsPanelTemplate x:Key="GridViewHorizontalTemplate">
    <WrapGrid Orientation="Horizontal" HorizontalAlignment="Center" MaximumRowsOrColumns="1" />
</ItemsPanelTemplate>

我不想在Scrollviewer中包装GridView,因为这样做会破坏我正在使用的内置增量加载。我意识到我可以创建一个ListView并切换它和GridView的可见性,但我宁愿不必绑定到额外的控件并复制其他相关代码。想法?

2 个答案:

答案 0 :(得分:0)

尝试将以下内容添加到GridView

<GridView.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical"/>
    </ItemsPanelTemplate>
</GridView.ItemsPanel>

答案 1 :(得分:0)

<GridView Name="display"  ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollMode="Enabled" ScrollViewer.VerticalScrollBarVisibility="Visible"  Height="550" Margin="0,0,0,0"  Grid.Row="1" RenderTransformOrigin="0.5,0.5"  SelectionMode="Single" >
    <GridView.RenderTransform>
        <CompositeTransform Rotation="0"/>
    </GridView.RenderTransform>
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <VariableSizedWrapGrid Orientation="Vertical" />
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
    <GridView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <StackPanel.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFD3D9DE" Offset="1"/>
                        <GradientStop Color="#FFFAFBFB"/>
                    </LinearGradientBrush>

                </StackPanel.Background>
                <Grid x:Name="grdResultTxtBlock" VerticalAlignment="Center" HorizontalAlignment="Left" Width="500" Height="250" Margin="0,0,0,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="55" />
                        <RowDefinition Height="55" />
                        <RowDefinition Height="70"  />
                        <RowDefinition Height="50" />
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0">
                        <Button  Content="{Binding Call}" Height="40" Style="{StaticResource PnxButtonStyle}"   HorizontalAlignment="Left"
                         Margin="0,10,200,0" x:Name="btnXmit" VerticalAlignment="Top" Width="122" />

                        <TextBlock  x:Name="txtAddress" FontWeight="Bold"  Text="{Binding Address}" Margin="150,0,400,0" HorizontalAlignment="Left"  VerticalAlignment="Center"   Width="200"  />
                        </Grid>
                    </Grid>
              </StackPanel>
            </DataTemplate>
    </GridView.ItemTemplate>
</GridView>