2016-01-21 70 views
1

我有一個ListView與分組。一切工作正常,除了滾動。如果我想向下滾動ListView,那麼在釋放ListView後可以回到開頭。我試圖設置ScrollViewer.VerticalScrollModeEnabledScrollViewer.VerticalScrollBarVisibilityAuto。但沒有成功。UWP - ListView滾動 - 不會停留在滾動位置

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> 

你能不能給我一些暗示?因爲我搜索了網絡,但沒有任何解決方案。提前致謝。

回答

5

的解決方案是在與PivotItemGrid取代StackPanel

<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> 
+0

非常感謝。我不會想到我自己。 – NSFW

0

如果我要釋放的ListView下來後,然後滾動ListView控件可以追溯到上開始

你可以聽垂直滾動條的滾動事件,並使用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); 
      } 
     } 
    }