WPF最简单的分页控件

背景:最近在写项目的时候需要写一个简单的分页功能,因项目需要,没有改为MVVM模式,只需要在后台实现

1、呈现效果如下:

 

 接下来就来上代码,看看怎么实现的

1、界面代码

 1                 <StackPanel Orientation="Horizontal"
 2                             HorizontalAlignment="Right">
 3                     <TextBlock TextWrapping="WrapWithOverflow"  
 4                                HorizontalAlignment="Right"
 5                                Margin="5,0,5,0"
 6                                VerticalAlignment="Center"
 7                                Text="{Binding OrderCount}">
 8                     </TextBlock>
 9                     <Button ToolTip="上一页"
10                             Width="30"
11                             Height="30"
12                             FontSize="16"
13                             Name="BtnPreviousPage"
14                             Background="#FFFFFF"
15                             Click="BtnPreviousPage_Click"
16                             Margin="5,0,5,0">
17                     </Button>
18                     <Border BorderBrush="#009639"
19                             BorderThickness="1"
20                             Width="30"   
21                             Height="30"
22                             Margin="5,0,5,0">
23                         <TextBlock TextWrapping="WrapWithOverflow" 
24                                    Text="{Binding CurrentPage}"
25                                    Foreground="#009639"
26                                    VerticalAlignment="Center"
27                                    HorizontalAlignment="Center"/>
28                     </Border>
29                     <Button ToolTip="下一页"
30                             Width="30"
31                             Height="30"
32                             FontSize="16"
33                             Background="#FFFFFF"
34                             Name="BtnNextPage"
35                             Click="BtnNextPage_Click"
36                             Margin="5,0,5,0">
37                     </Button>
38                 </StackPanel>
界面代码

2、后台代码

  1  
  2         private string _amountMoney;
  3 
  4         private string _orderCount;
  5 
  6         private int _currentPage = 1;
  7 
  8         private int _pageSize = 8;
  9 
 10         /// <summary>
 11         /// 未上传订单总数
 12         /// </summary>
 13         private int _count;
 14 
 15         List<OrderJMSStatus> mList = new List<OrderJMSStatus>();
 16 
 17         public int CurrentPage
 18         {
 19             get { return _currentPage; }
 20             set
 21             {
 22                 if (_currentPage != value)
 23                 {
 24                     _currentPage = value;
 25                     RaisePropertyChanged("CurrentPage");
 26                 }
 27             }
 28         }
 29 
 30         public string OrderCount
 31         {
 32             get { return _orderCount; }
 33             set
 34             {
 35                 if (_orderCount != value)
 36                 {
 37                     _orderCount = value;
 38                     RaisePropertyChanged("OrderCount");
 39                 }
 40             }
 41         }
 42 
 43         public string AmountMoney
 44         {
 45             get { return _amountMoney; }
 46             set
 47             {
 48                 if (_amountMoney != value)
 49                 {
 50                     _amountMoney = value;
 51                     RaisePropertyChanged("AmountMoney");
 52                 }
 53             }
 54         }
 55        public event PropertyChangedEventHandler PropertyChanged;
 56         public void RaisePropertyChanged(string propertyName)
 57         {
 58             if (PropertyChanged != null)
 59                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 60         }
 61         private int GetPageNumber(int count, int rowNumber)
 62         {
 63             if (count == 0 || rowNumber == 0)
 64             {
 65                 return 0;
 66             }
 67             int pageNumber = count / rowNumber;
 68             if (count % rowNumber != 0)
 69             {
 70                 pageNumber++;
 71             }
 72             return pageNumber;
 73         }
 74 
 75         private void BtnNextPage_Click(object sender, RoutedEventArgs e)
 76         {
 77 
 78             if (CurrentPage == GetPageNumber(_count, _pageSize))
 79             {
 80                 return;
 81             }
 82             
 83             List<OrderJMSStatus> result = mList.Skip(CurrentPage * _pageSize).Take(_pageSize).ToList();
 84 
 85             MessageList.Clear();
 86             MessageList = result;
 87             CurrentPage++;
 88         }
 89 
 90         private void BtnPreviousPage_Click(object sender, RoutedEventArgs e)
 91         {
 92             if (CurrentPage == 1)
 93             {
 94                 return;
 95             }
 96 
 97             List<OrderJMSStatus> result = new List<OrderJMSStatus>();
 98 
 99             if (CurrentPage == 2)
100             {
101                 result = mList.Take(_pageSize).ToList();
102             }
103             else
104             {
105                 result = mList.Skip((CurrentPage - 2) * _pageSize).Take(_pageSize).ToList();
106             }
107 
108             MessageList.Clear();
109             MessageList = result;
110 
111             CurrentPage--;
112         }
后台代码

实现过程基本上就是这样,希望能对你有所帮助

 

posted @ 2019-12-09 13:47  相约荞麦  Views(...)  Comments(...Edit  收藏