ListView paging operation

Reprinted from: ttps://blog.csdn.net/tiana0/article/details/51228290

In order to complete the ListView paging operation, this article will encapsulate a generic class PageInfo responsible for "paging operation". This generic class is not only suitable for the paging of ListView in WPF, but also for other controls in WPF and WinForms that require paging operation.
The following is the complete code of the PageInfo class (including the definition of the enumeration type JumpOperation).

    public class PageInfo<T>
    {
        public List<T> dataSource;
        int pageSize;
        int pagecount;
        int currentIndex;

        public PageInfo()
        {
            currentIndex = 1;
        }

        public PageInfo(List<T> dataSource, int pageSize)
            : this()
        {
            this.dataSource = dataSource;
            this.pageSize = pageSize;
            this.pagecount = dataSource.Count / pageSize;
            this.pagecount += (dataSource.Count % pageSize) != 0 ? 1 : 0;
        }

        public List<T> GetPageData(JumpOperation jo)
        {
            switch (jo)
            {
                case JumpOperation.GoHome:
                    currentIndex = 1;
                    break;
                case JumpOperation.GoPrePrevious:
                    if (currentIndex > 1)
                    {
                        currentIndex -= 1;
                    }
                    break;
                case JumpOperation.GoNext:
                    if (currentIndex < pagecount)
                    {
                        currentIndex += 1;
                    }
                    break;
                case JumpOperation.GoEnd:
                    currentIndex = pagecount;
                    break;
            }
            List<T> listPageData = new List<T>();
            try
            {
                int pageCountTo = pageSize;
                if (pagecount == currentIndex && dataSource.Count % pageSize > 0)
                {
                    pageCountTo = dataSource.Count % pageSize;
                }
                if (null != dataSource)
                {
                    for (int i = 0; i < pageCountTo; i++)
                    {
                        if ((currentIndex - 1) * pageSize + i < dataSource.Count)
                        {
                            listPageData.Add(dataSource[(currentIndex - 1) * pageSize + i]);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                return listPageData;
            }
            catch
            {
                return listPageData;
            }
        }
    }
    public enum JumpOperation
    {
        GoHome = 0,
        GoPrePrevious = 1,
        GoNext = 2,
        GoEnd =3
    }

When instantiating the PageInfo class, pass in the data source required by the ListView and the number of records displayed on each page, then calculate the total number of pages, and set the current page to 1.
The GetPageData method provided by the PageInfo class can be used to obtain the data source of the first page, the last page, the previous page and the next page.
Let's demonstrate how to use the PageInfo class.
(1) Front-end page code

<Window x:Class="ListViewSplitPages.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="8*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
        <ListView Grid.Row="0" x:Name="ListView_Students">
            <ListView.View>
                <GridView x:Name="GrdView_Students">
                    <GridViewColumn Width="100" Header="学号" DisplayMemberBinding="{Binding Path=ID}"/>
                    <GridViewColumn Width="100" Header="姓名" DisplayMemberBinding="{Binding Path=Name}"/>
                    <GridViewColumn Width="100" Header="班级" DisplayMemberBinding="{Binding Path=Class}"/>
                    <GridViewColumn Width="100" Header="年龄" DisplayMemberBinding="{Binding Path=Age}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="Button_Home" Content="首页" Grid.Column="0" Width="100" Click="Button_Home_Click"/>
            <Button x:Name="Button_Previous" Content="前一页" Grid.Column="1" Width="100" Click="Button_Previous_Click"/>
            <Button x:Name="Button_Next"  Content="下一页" Grid.Column="2" Width="100" Click="Button_Next_Click"/>
            <Button x:Name="Button_End" Content="尾页" Grid.Column="3" Width="100" Click="Button_End_Click"/>
        </Grid>
    </Grid>
</Window>

(2) The logic code of the front-end page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListViewSplitPages
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Student> studentList;
        PageInfo<Student> pageStudentList;
        public MainWindow()
        {
            InitializeComponent();
            studentList = new List<Student>() { 
                new Student(){ID="00000001",Name ="zsd",Class="1",Age="17"},
                new Student(){ID="00000002",Name ="fds",Class="3",Age="15"},
                new Student(){ID="00000003",Name ="ds",Class="1",Age="17"},
                new Student(){ID="00000004",Name ="fd",Class="3",Age="16"},
                new Student(){ID="00000005",Name ="rw",Class="1",Age="17"},
                new Student(){ID="00000006",Name ="hg",Class="1",Age="15"},
                new Student(){ID="00000007",Name ="dfd",Class="2",Age="16"},
                new Student(){ID="00000008",Name ="dfd",Class="2",Age="17"},
                new Student(){ID="00000009",Name ="gdf",Class="2",Age="17"},
                new Student(){ID="00000010",Name ="jh",Class="1",Age="15"},
                new Student(){ID="00000011",Name ="fdd",Class="2",Age="17"},
                new Student(){ID="00000012",Name ="rer",Class="1",Age="17"},
                new Student(){ID="00000013",Name ="vvv",Class="1",Age="15"},
                new Student(){ID="00000014",Name ="qww",Class="3",Age="16"},
                new Student(){ID="00000015",Name ="sds",Class="3",Age="18"},
                new Student(){ID="00000016",Name ="lk",Class="1",Age="17"},
                new Student(){ID="00000017",Name ="ui",Class="2",Age="16"},
                new Student(){ID="00000018",Name ="jkj",Class="1",Age="17"},
            };
            pageStudentList = new PageInfo<Student>(studentList, 5);
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoHome);
        }

        private void Button_Home_Click(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoHome);
        }

        private void Button_Previous_Click(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoPrePrevious);
        }

        private void Button_Next_Click(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoNext);
        }

        private void Button_End_Click(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoEnd);
        }
    }

    public class Student
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
        public string Age { get; set; }
    }
}

In the code, the student collection is used as the test data source, so the type parameter T of the generic class PageInfo is set to Student. Obviously, T can also be any other type.
When clicking on the "first page", "previous page", "next page" and "last page" in the screen, the GetPageData method of the PageInfo type will be called to obtain the data source.
The following is the screen after the program runs.

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324799794&siteId=291194637