Add Items to ListBox when scroll reaches the end in Windows phone?

拥有回忆 提交于 2019-12-12 15:29:49

问题


I need the requirement that..

Initially i have set of datas that are bound to ListBox... If we scroll to the end i will add some more datas to the collection and will update the ListBox... Is there any way to achieve this in Windows phone ?


回答1:


I suppose that by "achieve this" you mean the possibility to detect if the ListBox is at the end. In that case, this should help.

You'll first need to gain access to the ScrollViewer control in order to see if a user scrolled, and what the current position is. If my page is called ListContent, then this code should give you a good start:

public partial class ListContent
{
    private ScrollViewer scrollViewer;

    public ListContent()
    {
        InitializeComponent();
        Loaded += OnLoaded();
    }

    protected virtual void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        scrollViewer = ControlHelper.List<ScrollViewer>(lbItems).FirstOrDefault();
        if (scrollViewer == null) return;

        FrameworkElement framework = VisualTreeHelper.GetChild(viewer, 0) as FrameworkElement;
        if (framework == null) return;

        VisualStateGroup group = FindVisualState(framework, "ScrollStates");
        if (group == null) return;

        group.CurrentStateChanged += OnListBoxStateChanged;
    }

    private VisualStateGroup FindVisualState(FrameworkElement element, string name)
    {
        if (element == null)
            return null;

        IList groups = VisualStateManager.GetVisualStateGroups(element);
        return groups.Cast<VisualStateGroup>().FirstOrDefault(@group => @group.Name == name);
    }

    private void OnListBoxStateChanged(object sender, VisualStateChangedEventArgs e)
    {
        if (e.NewState.Name == ScrollState.NotScrolling.ToString())
        {
            // Check the ScrollableHeight and VerticalOffset here to determine
            // the position of the ListBox.
            // Add items, if the ListBox is at the end.

            // This event will fire when the listbox complete stopped it's 
            // scrolling animation
        }
    }
}

If you're talking about adding the data dynamically, make sure you are using an ObservableCollection for your data. Added items will automatically show up in your ListBox.



来源:https://stackoverflow.com/questions/10930533/add-items-to-listbox-when-scroll-reaches-the-end-in-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!