在Windows Phone 7中还原列表框的确切滚动位置 [英] Restoring exact scroll position of a listbox in Windows Phone 7

查看:105
本文介绍了在Windows Phone 7中还原列表框的确切滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使应用程序因被删除而很好地返回.该应用程序包含较大的列表框,因此理想情况下,我想回滚到用户在这些列表框周围滚动时所处的位置.

I'm working on making an app come back nicely from being tombstoned. The app contains large listboxes, so I'd ideally like to scroll back to wherever the user was while they were scrolling around those listboxes.

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

It's easy to jump back to a particular SelectedItem - unfortunately for me, my app never needs the user to actually select an item, they're just scrolling through them. What I really want is some sort of MyListbox.ScrollPositionY but it doesn't seem to exist.

有什么想法吗?

克里斯

推荐答案

您需要内部掌握ListBox所使用的ScrollViewer,以便可以获取VerticalOffset属性的值,并随后获取调用SetVerticalOffset方法.

You need to get hold of the ScrollViewer that is used by the ListBox internally so you can grab the value of the VerticalOffset property and subsequently call the SetVerticalOffset method.

这要求您从ListBox向下穿过组成其内部的可视树.

This requires that you reach down from the ListBox through the Visual tree that makes up its internals.

我使用了这个方便的扩展类,您应该将其添加到您的项目中(由于必须不断重复,因此我必须将其放在博客中):-

I use this handy extension class which you should add to your project (I've gotta put this up on a blog because I keep repeating it):-

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(以及与此相关的所有其他UIElement)将获得几个新的扩展方法DescendentsAncestors.我们可以将它们与Linq结合起来搜索内容.在这种情况下,您可以使用:-

With this available the ListBox (and all other UIElements for that matter) gets a couple of new extension methods Descendents and Ancestors. We can combine those with Linq to search for stuff. In this case you could use:-

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

这篇关于在Windows Phone 7中还原列表框的确切滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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