计算水平偏移以将ListView滚动到SelectedItem的中心

我正在构建一个照片应用程序,使用FlipViewlistView作为分页。 当我点击ListView的缩略图时,它会在FlipView显示相同的图片。 当我滑入FlipView ,所选的任何照片都会在ListView选择相同的图片。 这可以通过添加到它们来完成:

ListView

 SelectedIndex="{Binding Path=SelectedIndex, ElementName=flipView1, Mode=TwoWay} 

FlipView

 SelectedIndex="{Binding Path=SelectedIndex, ElementName=listView1, Mode=TwoWay} 

我添加了ListView SelectionChanged事件:

  if (e.AddedItems.Count > 0) listView1.ScrollIntoView(e.AddedItems.First(), ScrollIntoViewAlignment.Leading); 

我唯一的问题是当我滑动FlipView ,在ListView选择了所需的图片,但ScrollViewer没有滚动到它。 我尝试使用WinRTXamlToolkit来更改ScrollViewer的位置:

 private void pageRoot_Loaded() { // count number of all items int itemCount = this.listView1.Items.Count; if (itemCount == 0) return; if (listView1.SelectedIndex >= itemCount) listView1.SelectedIndex = itemCount - 1; // calculate x-posision of selected item double listWidth = this.listView1.ActualWidth; double xPos = (listWidth / itemCount) * listView1.SelectedIndex; // scroll var scrollViewer2 = listView1.GetFirstDescendantOfType(); if (scrollViewer2 != null) scrollViewer2.ChangeView(xPos, 0.0, 1); } 

第一次listWidth1600.0然后它一直变为0.0 ,这给了xPos = 0.0

我怎样才能解决这个问题?

https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.listview.aspx

您应该使用两种“ScrollIntoView”方法之一。

ListView.ScrollIntoView()应该工作。 在滚动ScrollViewer时,可能存在滚动ScrollViewer的问题。 我会尝试摆弄ScrollViewer.InvalidateScrollInfo() ,这可能会加速它。 否则 – 您可以尝试处理ViewChanging/ViewChanged事件以查看它是否正在滚动并尝试将该信息与ScrollViewerViewChangedEventArgs.IsIndeterminate一起使用以链接调用。

另请查看我对此问题的回答: 在滚动查看器中居中选定的项目