[英]Calculate Horizontal Offset to scroll ListView to the center of the SelectedItem
I am building a photo application, using a FlipView
and a listView
as a Pagination. 我正在构建一个照片应用程序,使用FlipView
和listView
作为分页。 When I click on the thumbnail picture in the ListView
it shows me the same picture in the FlipView
. 当我点击ListView
的缩略图时,它会在FlipView
显示相同的图片。 And when I swipe into the FlipView
, any photo selected will select the same picture in the ListView
. 当我滑入FlipView
,所选的任何照片都会在ListView
选择相同的图片。 This is done by adding to both of them: 这是通过添加到它们两个来完成的:
To the ListView
: 到ListView
:
SelectedIndex="{Binding Path=SelectedIndex, ElementName=flipView1, Mode=TwoWay}
And to the FlipView
: 到FlipView
:
SelectedIndex="{Binding Path=SelectedIndex, ElementName=listView1, Mode=TwoWay}
And to the ListView SelectionChanged
event I added: 我添加了ListView SelectionChanged
事件:
if (e.AddedItems.Count > 0)
listView1.ScrollIntoView(e.AddedItems.First(), ScrollIntoViewAlignment.Leading);
My only problem is that when I swipe the FlipView
, the desired picture is selected in the ListView
but the ScrollViewer
is not scrolled to it. 我唯一的问题是当我滑动FlipView
,在ListView
选择了所需的图片,但ScrollViewer
没有滚动到它。 I tried using WinRTXamlToolkit
to change the position of the 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<ScrollViewer>();
if (scrollViewer2 != null)
scrollViewer2.ChangeView(xPos, 0.0, 1);
}
The first time listWidth
is 1600.0
and then it becomes 0.0
all the time, which gives xPos = 0.0
! 第一次listWidth
是1600.0
然后它一直变为0.0
,这给了xPos = 0.0
!
How can I fix this? 我怎样才能解决这个问题?
https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.listview.aspx https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.listview.aspx
You should use one of the two "ScrollIntoView" methods. 您应该使用两个“ScrollIntoView”方法之一。
ListView.ScrollIntoView()
should work. ListView.ScrollIntoView()
应该工作。 There might be issues with calling a method to scroll a ScrollViewer
while it's already scrolling though. 在滚动ScrollViewer
时,可能存在滚动ScrollViewer
的问题。 I would try fiddling with ScrollViewer.InvalidateScrollInfo()
which might speed it up. 我会尝试摆弄ScrollViewer.InvalidateScrollInfo()
,这可能会加速它。 Otherwise - you could try handling the ViewChanging/ViewChanged
events to see if it's scrolling and try to use that information together with ScrollViewerViewChangedEventArgs.IsIndeterminate
to chain the calls. 否则 - 您可以尝试处理ViewChanging/ViewChanged
事件以查看它是否正在滚动并尝试将该信息与ScrollViewerViewChangedEventArgs.IsIndeterminate
一起使用以链接调用。
Also check my answer to this question: Centering selected item in a scroll viewer 另请查看我对此问题的回答: 在滚动查看器中居中选定的项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.