简体   繁体   English

如何限制wp8.1中的ListView项的内容-分页概念

[英]How to limit the contents of ListView Items in wp8.1 - Pagination Concept

Iam working on WP8.1 Project I want to Display contents in ListBox. 我正在WP8.1项目上工作,我想在ListBox中显示内容。 By default the listbox will show 5 Items, when the user scrolls to 7th item it should load next 10 items the loop should continue till all the data are displayed(Example: Same like how the scroll box size in Excel Sheet gets minimized when we scroll down) 默认情况下,列表框将显示5个项目,当用户滚动到第7个项目时,它应加载下一个10个项目,循环应继续进行直到显示所有数据为止(示例:就像我们滚动时如何缩小Excel工作表中的滚动框大小一样下)

for that you require two important things. 为此,您需要两项重要的工作。

1) limit the items in the List (or any Collection ) that you're going to assign as the itemssource to the ListBox . 1)限制将要分配为ListBox的itemssource的List (或任何Collection )中的项目。

2) you have to detect the scroll reaching at the end of scroll viewer of listbox. 2)您必须检测滚动到达列表框的滚动查看器的末尾。

Now, eg. 现在,例如。 your XAML is this: 您的XAML是这样的:

    <ListBox x:Name="lbx">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

and here is rest of code behind part. 这是部分代码的其余部分。

    // two lists are for assigning ItemsSource and
    // other is for all items to represent.

    List<string> lstInt = new List<string>();
    List<string> lstIntFull = new List<string>();
    void BlankPage3_Loaded(object sender, RoutedEventArgs e)
    {
        // below code will get ScrollViewer from ListBox and define the event handler to it's ViewChanged event.

        sv = VisualTreeHelper.GetChild((VisualTreeHelper.GetChild(lbx, 0) as Border), 0) as ScrollViewer;
        sv.ViewChanged += sv_ViewChanged;

        for (int i = 0; i < 100; i++)
        {
            lstIntFull.Add(i.ToString());
        }

        for (int i = 0; i <= 10; i++)
        {
            lstInt.Add(lstIntFull[i]);
        }

        lbx.ItemsSource = lstInt;
    }

    // event handler of ViewChanged event that we declared on PageLoad
    void sv_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
    {
        var verticalOffset = sv.VerticalOffset;
        var maxVerticalOffset = sv.ScrollableHeight; //sv.ExtentHeight - sv.ViewportHeight;

        if (maxVerticalOffset < 0 ||
            verticalOffset == maxVerticalOffset)
        {

            // Scrolled to bottom
            // So, write the code here to load next set of data

            var itemCount = (lbx.ItemsSource as List<string>).Count;
            for (int i = itemCount; i <= itemCount + 10; i++)
            {
                if (i < 100)
                {
                    lstInt.Add(lstIntFull[i]);
                }
            }

            // await Task.Delay(2000);

            // lbx.ItemsSource = null;
            lbx.ItemsSource = lstInt;
        }
        else
        {
            // Not scrolled to bottom

        }
    }

Now compare your example with this one. 现在将您的示例与此示例进行比较。 and follow the steps to your answer. 并按照步骤回答。

This Concept is called Pagination . 这个概念称为Pagination

Check this link to find out how to detect the ScrollViewer reaches at the End of Scrollable height. 检查此链接以了解如何检测ScrollViewer达到可滚动高度的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM