算算水平偏移滚动的ListView到的SelectedItem中心 [英] Calculate Horizontal Offset to scroll ListView to the center of the SelectedItem
问题描述
我建立一个照片应用程序,使用 FlipView
和的ListView
为分页。
当我点击了的ListView缩小图片
它显示我在 FlipView
相同的画面。当我刷卡进入 FlipView
,选择将选择的ListView相同的图片任何照片
。这是通过添加到两者完成:
I am building a photo application, using a FlipView
and a listView
as a Pagination.
When I click on the thumbnail picture in the ListView
it shows me the same picture in the FlipView
. And when I swipe into the FlipView
, any photo selected will select the same picture in the ListView
. This is done by adding to both of them:
到的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
:
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. I tried using WinRTXamlToolkit
to change the position of the 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);
}
首次 listWidth
是 1600.0
然后把它变成 0.0
所有的时间,这给 XPOS = 0.0
!
The first time listWidth
is 1600.0
and then it becomes 0.0
all the time, which gives xPos = 0.0
!
我该如何解决这个问题?
How can I fix this?
推荐答案
ListView.ScrollIntoView()
应该工作。有可能是与调用一个方法来滚动的ScrollViewer问题
,而它已经滚动,虽然。我会尝试用摆弄ScrollViewer.InvalidateScrollInfo()
这可能会加快速度。否则 - 你可以尝试处理 ViewChanging / ViewChanged
事件来看看它的滚动和尝试一起使用该信息与 ScrollViewerViewChangedEventArgs.IsIndeterminate
来链的调用。
ListView.ScrollIntoView()
should work. There might be issues with calling a method to scroll a ScrollViewer
while it's already scrolling though. I would try fiddling with ScrollViewer.InvalidateScrollInfo()
which might speed it up. 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.
还检查我的回答这个问题:
<一href=\"http://stackoverflow.com/questions/22595675/centering-selected-item-in-a-scroll-viewer\">Centering在滚动查看器选择的项目
Also check my answer to this question: Centering selected item in a scroll viewer
这篇关于算算水平偏移滚动的ListView到的SelectedItem中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!