fire (call) method, when scrollviewer scrolled to its end

久未见 提交于 2019-12-08 11:59:20

问题


after being googled a lot, finally i am going to ask this to you guys.
i'v created scrollviewer, which contains items(listboxItems) and these items fetching from the webservices. but at once only 5 items can fetched. so initially it'll be 5 items, then next 5(appended to the scrollviewer) and then next 5 and so on......

note:-here listBoxitems are used inside the scrollviewer, not the listbox

data fetched from webservice also contains--->

total items
numResults (current number of items fetched) 

now suppose my method to fetch data is--->

getData(int nextResult)
{
      // some code to fetch the data
}

where nextResult is next item number(ex:- nextResult is 6, if requesting 2nd time)

but i am unable to write the code to detect that the user scrolled the scrollviewer to the end & then a method to be called or fired, whatever say!

i'v been badly confused among scrollviewer's VerticalOffset, ExtentHeight, ViewportHeight, ScrollableHeight etc, about to use them & calculate desired information to achieve above requirements.

so if anyone knows about the same or used ever scrollviwer, than please post answer.


回答1:


I achieved it by registering new DependencyProperty ListVerticalOffset with appropriate event:

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        ListVerticalOffsetProperty = DependencyProperty.Register("ListVerticalOffset", typeof(double), typeof(MainPage), new PropertyMetadata(OnListVerticalOffsetChanged));

        YourScrollViewer.Loaded += YourScrollViewer_Loaded;
    }

    void YourScrollViewer_Loaded(object sender, RoutedEventArgs e)
    {
        var binding = new Binding
            {
                Source = YourScrollViewer,
                Path = new PropertyPath("VerticalOffset"),
                Mode = BindingMode.OneWay
            };
        SetBinding(ListVerticalOffsetProperty, binding);
    }

    private void OnListVerticalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var atBottom = YourScrollViewer.VerticalOffset >= YourScrollViewer.ScrollableHeight;
        if (atBottom) MessageBox.Show("End");
    }

    public readonly DependencyProperty ListVerticalOffsetProperty;

    public double ListVerticalOffset
    {
        get { return (double)GetValue(ListVerticalOffsetProperty); }
        set { SetValue(ListVerticalOffsetProperty, value); }
    }

May be this solution or it's realization is not optimal, but it's working for me.



来源:https://stackoverflow.com/questions/15944410/fire-call-method-when-scrollviewer-scrolled-to-its-end

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