Help classes that support data listing pagination

Source: Internet
Author: User

Support for paging of MVVM components you can find online, old week This class is just nothing to write entertainment, mainly functional simple, lightweight, at least it satisfies my needs, and perhaps there are unknown bugs.

This class supports paging through data lists by using the Skip and take extension methods to extract a piece of data from the source list. When instantiating, you need to provide a Ienumerable<t> object as a parameter that this class will use to calculate pagination based on this data source, using the parameter T to support generics.

        Public pagabledcollection (ienumerable<t> srcitems)        {            _containeritems = Srcitems;            Total number of items            _totalitems = _containeritems.count ();            Calculates the total number of pages            computepages ();            CurrentPage = 1; Default Page        }

The private field _containeritems is used primarily to refer to the list of source data, _totalitems represents the total number of all data, the CurrentPage attribute represents the index of the current page, in general, the page is starting from page 1th, the index of the current page is the minimum value of 1, The maximum is the total number of pages.

The Computepages method is used to calculate the total number of pages with the following code:

        private void Computepages ()        {            int p = totalitems/pagesize;            if ((totalitems% PageSize) > 0)            {                p++;            }            TotalPages = p;            ...        }

Divide the total number of data by the number of bars per page (PageSize) to get pages, but be aware that the remainder is the problem. For example, the total number of data is 10, each page shows 3 data, then 10/3 of the result is 3, the remainder is 1, this time, the total page should be 4, not 3. So the above code after the division operation to check, if there is a remainder, gross position pages plus 1.

The CurrentPage property represents the current page index and, when modified, extracts a piece of data from the source data list and exposes it with the Pageditems property.

        public int currentpage        {            get {return _currentpage;}            Set            {                if (_currentpage! = value)                {                    if (value < 1) _currentpage = 1;                    else if (value > TotalPages) _currentpage = totalpages;                    else _currentpage = value;                    Filter content                    Setpageditemscore ();                    Checkpaging ();                    OnPropertyChanged (nameof (CurrentPage));}}        

As mentioned above, the minimum value of the current page index is 1 and the maximum is the total number of pages, so the set property is validated. Setpageditemscore

The function of a method is to filter a piece of data from the source data list based on the current page index. The method is defined as follows:

        private void Setpageditemscore ()        {            var r = _containeritems.skip ((CurrentPage-1) * PageSize). Take (PageSize);            Pageditems = r;        }

The starting position of the data from the source data List = (Current page-1) * Number of displays per page, the amount of data to extract = PageSize, that is, the number of displays per page.

When used, the type is instantiated directly, and the data source is passed in from the constructor and then bound to the UI.

            list<int> list = new list<int> ();            for (int x=1; x<=; x + +)            {                list. ADD (x);            }            pagabledcollection<int> cols = new pagabledcollection<int> (list);            Cols. PageSize = n;            Rootlayout. DataContext = cols;

In a XAML document, however, you bind directly to the individual properties of the Pagabledcollection instance.

    <grid margin= "name=" Rootlayout "> <Grid.RowDefinitions> <rowdefinition/> <rowdefinition height= "Auto"/> </Grid.RowDefinitions> <listbox margin= "3" itemssource= " {Binding Pageditems} "/> <stackpanel grid.row=" 1 "orientation=" horizontal "> <button content=" Prev "margin=" 0,0,5,0 "isenabled=" {Binding canpageup} "click=" Onpageup "/> <TextBlock> &l T  Textblock.text> <multibinding stringformat= "{0} page/total {1} pages" > <binding Path= "CurrentPage"/> <binding path= "TotalPages"/> </multibinding&gt                ; </TextBlock.Text> </TextBlock> <button content= "Next" margin= "5,0,0,0" isenabled= "{Bin            Ding Canpagedown} "click=" Onpagedown "/> <textblock margin=" 3,0,0,0 "> show number of bars per page:</textblock> &lT TextBox width= "text=" {Binding path=pagesize,mode=twoway,updatesourcetrigger=propertychanged} "/> <Butt On content= "Show All" click= "Onshowall" margin= "13,0,0,0"/> </StackPanel> </Grid>

Finally, the operation of the program, the effect is basically satisfactory.

This class, writing is not a professional, the overall level of entertainment, it is for everyone to do the introductory study reference bar.

Sample source code Download

Classification:WPF, personal articles Label:WPF, paging

Help classes that support data listing pagination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.