微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight – 在Windows Phone 7中恢复列表框的确切滚动位置

我正在努力让一个应用程序很好地从墓碑式回来.该应用程序包含大型列表框,所以我最好滚动回到用户在这些列表框中滚动时的位置.

跳回到特定的SelectedItem很容易 – 不幸的是,对我来说,我的应用程序从不需要用户实际选择项目,他们只是滚动它们.我真正想要的是某种MyListBox.ScrollPositionY但它似乎不存在.

有任何想法吗?

克里斯

解决方法

您需要在内部获取ListBox使用的ScrollViewer,以便您可以获取VerticalOffset属性的值,然后调用SetVerticalOffset方法.

这要求您通过构成其内部的Visual树从ListBox向下到达.

我使用这个方便的扩展类,你应该添加到你的项目中(我必须把它放在博客上因为我不断重复它): –

public static class VisualTreeEnumeration
{
    public static IEnumerable<DependencyObject> Descendents(this DependencyObject root,int depth)
    {
        int count = VisualTreeHelper.GetChildrenCount(root);
        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(root,i);
            yield return child;
            if (depth > 0)
            {
                foreach (var descendent in Descendents(child,--depth))
                    yield return descendent;
            }
        }
    }

    public static IEnumerable<DependencyObject> Descendents(this DependencyObject root)
    {
        return Descendents(root,Int32.MaxValue);
    }

    public static IEnumerable<DependencyObject> Ancestors(this DependencyObject root)
    {
        DependencyObject current = VisualTreeHelper.GetParent(root);
        while (current != null)
        {
            yield return current;
            current = VisualTreeHelper.GetParent(current);
        }
    }
}

有了这个,ListBox(以及所有其他UIElements)就可以得到一些新的扩展方法Descedents和Ancestors.我们可以将这些与Linq结合起来搜索东西.在这种情况下,您可以使用: –

ScrollViewer sv = SomeListBox.Descendents().OfType<ScrollViewer>().FirstOrDefault();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐