计算水平偏移以将ListView滚动到SelectedItem的中心
我正在构建一个照片应用程序,使用FlipView
和listView
作为分页。 当我点击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); }
第一次listWidth
是1600.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
一起使用以链接调用。
另请查看我对此问题的回答: 在滚动查看器中居中选定的项目