如何在 WP8 中获取 LongListSelector 的 VerticalOffset [英] How can I get the VerticalOffset of a LongListSelector in WP8

查看:15
本文介绍了如何在 WP8 中获取 LongListSelector 的 VerticalOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WP7 中,LongListSelector 有一个底层 ScrollViewer,我可以从中恢复列表的垂直偏移.但在 Windows Phone 8 中,没有底层 ScrollViewer 或任何类似的类为我提供 VerticalOffset 属性.

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 ScrollViewer nor any similar class that provides me with that VerticalOffset property.

我一直在寻找,但没有找到任何东西.我可以使用一个函数来提供列表中的第一个可见元素,但我也没有找到任何东西.ItemRealized 事件对此没有用处,因为它没有提供显示在视口顶部的确切项目.

I've been searching and didn't find anything. I could settle with a function that gives the first visible element in the list, but I haven't found anything either. The ItemRealized event is not useful for that, as it doesn't give the exact item that is being shown on top of the viewport.

推荐答案

这将为您提供 LLS 中的第一个可见项目.

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;
}

这篇关于如何在 WP8 中获取 LongListSelector 的 VerticalOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆