How can I get the VerticalOffset of a LongListSelector in WP8

后端 未结 1 939
名媛妹妹
名媛妹妹 2020-12-28 23:47

In WP7, the LongListSelector had a underlying ScrollViewer, from which I could recover the vertical offset of the list. But in Windows Phone 8, there\'s no underlying Scroll

相关标签:
1条回答
  • 2020-12-29 00:27

    This will give you the first visible item in the 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;
    }
    
    0 讨论(0)
提交回复
热议问题