這會給你LLS中的第一個可見項目。
private Dictionary<object, ContentPresenter> items;
private object GetFirstVisibleItem(LongListSelector lls)
{
var offset = FindViewport(lls).Viewport.Top;
return items.Where(x => Canvas.GetTop(x.Value) + x.Value.ActualHeight > offset)
.OrderBy(x => Canvas.GetTop(x.Value)).First().Key;
}
private void LLS_ItemRealized(object sender, ItemRealizationEventArgs e)
{
if (e.ItemKind == LongListSelectorItemKind.Item)
{
object o = e.Container.DataContext;
items[o] = e.Container;
}
}
private void LLS_ItemUnrealized(object sender, ItemRealizationEventArgs e)
{
if (e.ItemKind == LongListSelectorItemKind.Item)
{
object o = e.Container.DataContext;
items.Remove(o);
}
}
private static ViewportControl FindViewport(DependencyObject parent)
{
var childCount = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < childCount; i++)
{
var elt = VisualTreeHelper.GetChild(parent, i);
if (elt is ViewportControl) return (ViewportControl)elt;
var result = FindViewport(elt);
if (result != null) return result;
}
return null;
}
在黑暗中拍攝 - 但我昨天也有類似的問題。看看答案。也許你可以用'LongListSelector'來代替ScrollViewer?你不需要調用'ScrollToVerticalOffset()',但你可能做一些類似的事情來獲得偏移量?只是一個想法! http://stackoverflow.com/questions/15114991/how-to-use-scrollviewer-scrolltoverticaloffset – lhan 2013-02-27 21:59:16
問題是,我不能用ScrollViewer替換LLS,我需要ItemsSource綁定,並且自己做它不是不錯的選擇。不過謝謝。 – gjulianm 2013-02-28 10:26:08