UWP - ListView滚动 - 不会停留在滚动位置

时间:2016-01-21 20:45:48

标签: xaml windows-10 uwp

我有ListView分组。一切都很好,除了滚动。如果我想向下滚动ListView,那么在释放后ListView会回到开头。我尝试将ScrollViewer.VerticalScrollMode设置为Enabled,将ScrollViewer.VerticalScrollBarVisibility设置为Auto。但没有成功。

XAML代码:

<PivotItem>
    <StackPanel Orientation="Vertical" >    
        <TextBox x:Name="SearchBox"
                     PlaceholderText="Search ... "
                     TextChanged="SearchBox_TextChanged"/>

        <ListView x:Name="ContactsListView"
                  ItemTemplate="{StaticResource ContactsTemplate}"
                  ItemsSource="{x:Bind ContactsViewSource.View}"
                  SelectionMode="Single"
                  ScrollViewer.VerticalScrollMode="Enabled"
                  ScrollViewer.VerticalScrollBarVisibility="Auto" >

                  <ListView.GroupStyle>
                     <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                           <DataTemplate x:DataType="data:GroupingItem">
                              <TextBlock Text="{x:Bind Key}"
                                         Foreground="Blue"
                                         Style="{ThemeResource TitleTextBlockStyle}"/>
                           </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                     </GroupStyle>
                  </ListView.GroupStyle>
        </ListView>
    </StackPanel>
</PivotItem>
你可以给我一些提示吗?因为我在网上搜索但没有任何解决方案。提前谢谢。

2 个答案:

答案 0 :(得分:5)

解决方案是将StackPanel替换为Grid中的PivotItem

<PivotItem>
    <Grid>
       <Grid.RowDefinitions>
           <RowDefinition Height="50" />
           <RowDefinition Height="*" />
       </Grid.RowDefinitions>
        <TextBox x:Name="SearchBox"
                     PlaceholderText="Search ... "
                     TextChanged="SearchBox_TextChanged"
                     Grid.Row="0"/>

        <ListView x:Name="ContactsListView"
                  ItemTemplate="{StaticResource ContactsTemplate}"
                  ItemsSource="{x:Bind ContactsViewSource.View}"
                  SelectionMode="Single"
                  Grid.Row="1" >

                  <ListView.GroupStyle>
                     <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                           <DataTemplate x:DataType="data:GroupingItem">
                              <TextBlock Text="{x:Bind Key}"
                                         Foreground="Blue"
                                         Style="{ThemeResource TitleTextBlockStyle}"/>
                           </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                     </GroupStyle>
                  </ListView.GroupStyle>
        </ListView>
   </Grid>
</PivotItem>

答案 1 :(得分:0)

  

如果我想向下滚动ListView,那么在释放ListView后回到开头

您可以收听垂直ScrollBar的Scroll事件,并使用ScrollViewer实际调整ScrollBar位置,当它位于底部时。

        var scrollViewer = GetDescendants(ContactsListView).OfType<ScrollViewer>().FirstOrDefault();
        var verticalScrollbar = GetDescendants(scrollViewer).OfType<ScrollBar>().FirstOrDefault(x => x.Orientation == Orientation.Vertical);
        verticalScrollbar.Scroll += (o, e) =>
        {
            if (e.ScrollEventType != ScrollEventType.EndScroll)
                return;

            if (e.NewValue >= verticalScrollbar.Maximum)
                scrollViewer.ScrollToVerticalOffset(0); // Scroll to the top
        };

下面是助手类,您可以使用它来导航ListView可视树以查找ScrollViewer和Vertical ScrollBar。

    public static IEnumerable<DependencyObject> GetDescendants(DependencyObject start)
    {
        var queue = new Queue<DependencyObject>();
        var count = VisualTreeHelper.GetChildrenCount(start);

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(start, i);
            yield return child;
            queue.Enqueue(child);
        }

        while (queue.Count > 0)
        {
            var parent = queue.Dequeue();
            var count2 = VisualTreeHelper.GetChildrenCount(parent);

            for (int i = 0; i < count2; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                yield return child;
                queue.Enqueue(child);
            }
        }
    }