WP7 中的 ListBox 偏移量 [英] ListBox offset in WP7

查看:20
本文介绍了WP7 中的 ListBox 偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为 ListBox 设置偏移量?我所能找到的只是滚动到一个元素,但我需要将 ListBox 滚动到任何位置.

It is possible set the offset for a ListBox? All that I can find is scroll to an element, but I need to scroll the ListBox to any position.

作为替代,还有其他任何组件可以使他们的项目虚拟化,并且我可以控制偏移量?

As an alternative, there are any other component that can make virtualize their items, and that I can control the offset?

推荐答案

您可以获得 ListBox 的 ScrollViewer 并使用它的 ScrollToVerticalOffset-method. 要获得 ScrollViewer,您可以例如连接到 ListBox 的 Loaded-event,如以下:

You can get the ListBox's ScrollViewer and use its ScrollToVerticalOffset-method. To get the ScrollViewer, you can for example hook up to the ListBox's Loaded-event like the following:

XAML:

<ListBox Loaded="HookScrollViewer">

代码隐藏:

    private void HookScrollViewer(object sender, RoutedEventArgs e)
    {
        var element = (FrameworkElement)sender;
        var scrollViewer = ControlHelpers.FindChildOfType<ScrollViewer>(element);

        if (scrollViewer == null)
            return;

        this.myScrollViewer = scrollViewer;
    }

ControlHerlpers.FindChildOfType 方法是这样实现的:

The ControlHerlpers.FindChildOfType-method is implement this way:

    public static T FindChildOfType<T>(DependencyObject root) where T : class
    {
        var queue = new Queue<DependencyObject>();
        queue.Enqueue(root);

        while (queue.Count > 0)
        {
            var current = queue.Dequeue();
            for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
            {
                var child = VisualTreeHelper.GetChild(current, i);
                var typedChild = child as T;
                if (typedChild != null)
                {
                    return typedChild;
                }
                queue.Enqueue(child);
            }
        }
        return null;
    }

现在您在 myScrollViewer 成员中有 ListBox 的 ScrollViewer,您可以直接访问它的方法.例如,要滚动底部,您可以调用:

Now you have the ListBox's ScrollViewer in the myScrollViewer member and you can directly access its methods. For example, to scroll bottom you can call:

this.myScrollViewer.ScrollToVerticalOffset(double.MaxValue);

这篇关于WP7 中的 ListBox 偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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