WPF里DataGrid分页控件

1.主要代码:

  1 using System;
  2 using System.Collections.ObjectModel;
  3 using System.Windows;
  4 using System.Windows.Controls;
  5 using System.Windows.Input;
  6 using GalaSoft.MvvmLight.Command;
  7 
  8 namespace PaggingDataGrid.CustomCtrl
  9 {
 10     /// <summary>按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 元素中: xmlns:MyNamespace="clr-namespace:PaggingDataGrid" 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 元素中: xmlns:MyNamespace="clr-namespace:PaggingDataGrid;assembly=PaggingDataGrid" 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用, 并重新生成以避免编译错误: 在解决方案资源管理器中右击目标项目,然后依次单击 “添加引用”->“项目”->[浏览查找并选择此项目] 步骤 2) 继续操作并在 XAML 文件中使用控件。
 11     /// <MyNamespace:PaggingControl />
 12     /// </summary>
 13     public class PaggingControl : Control
 14     {
 15         // Using a DependencyProperty as the backing store for TotalPages.  This enables animation, styling, binding, etc...
 16         public static readonly DependencyProperty TotalPagesProperty =
 17             DependencyProperty.Register("TotalPages", typeof (uint), typeof (PaggingControl),
 18                                         new PropertyMetadata(1u, TotalPagesPropertyChangeCallback));
 19 
 20         // Using a DependencyProperty as the backing store for CurrentPage.  This enables animation, styling, binding, etc...
 21         public static readonly DependencyProperty CurrentPageProperty =
 22             DependencyProperty.Register("CurrentPage", typeof (uint), typeof (PaggingControl),
 23                                         new PropertyMetadata(1u, CurrentPagePropertyChangeCallback));
 24 
 25         // Using a DependencyProperty as the backing store for PageSize.  This enables animation, styling, binding, etc...
 26         public static readonly DependencyProperty PageSizeProperty =
 27             DependencyProperty.Register("PageSize", typeof (uint), typeof (PaggingControl),
 28                                         new PropertyMetadata(10u, PageSizePropertyChangecallback));
 29 
 30         // Using a DependencyProperty as the backing store for PageSizeList.  This enables animation, styling, binding, etc...
 31         public static readonly DependencyProperty PageSizeListProperty =
 32             DependencyProperty.Register("PageSizeList", typeof (ObservableCollection<uint>), typeof (PaggingControl),
 33                                         new PropertyMetadata(new ObservableCollection<uint> {10u, 20u, 50u, 100u}));
 34 
 35         // Using a DependencyProperty as the backing store for ItemsCount.  This enables animation, styling, binding, etc...
 36         public static readonly DependencyProperty ItemsCountProperty =
 37             DependencyProperty.Register("ItemsCount", typeof (uint), typeof (PaggingControl),
 38                                         new PropertyMetadata(1u, ItemsCountPropertyChangeCallback));
 39 
 40         // Using a DependencyProperty as the backing store for PageRefreshCommand.  This enables animation, styling, binding, etc...
 41         public static readonly DependencyProperty PageRefreshCommandProperty =
 42             DependencyProperty.Register("PageRefreshCommand", typeof (ICommand), typeof (PaggingControl),
 43                                         new PropertyMetadata(null));
 44 
 45         static PaggingControl()
 46         {
 47             DefaultStyleKeyProperty.OverrideMetadata(typeof (PaggingControl),
 48                                                      new FrameworkPropertyMetadata(typeof (PaggingControl)));
 49         }
 50 
 51         public PaggingControl()
 52         {
 53             //第一次时候 刷新一下列表
 54             Loaded += delegate { RaisePageRefreshEvent(); };
 55         }
 56 
 57         /// <summary>总页数</summary>
 58         public uint TotalPages
 59         {
 60             get { return (uint) GetValue(TotalPagesProperty); }
 61             set { SetValue(TotalPagesProperty, value); }
 62         }
 63 
 64         /// <summary>当前页</summary>
 65         public uint CurrentPage
 66         {
 67             get { return (uint) GetValue(CurrentPageProperty); }
 68             set { SetValue(CurrentPageProperty, value); }
 69         }
 70 
 71         /// <summary>每页的大小</summary>
 72         public uint PageSize
 73         {
 74             get { return (uint) GetValue(PageSizeProperty); }
 75             set { SetValue(PageSizeProperty, value); }
 76         }
 77 
 78         /// <summary>每页大小列表,即页面大小选择框的数据源</summary>
 79         public ObservableCollection<uint> PageSizeList
 80         {
 81             get { return (ObservableCollection<uint>) GetValue(PageSizeListProperty); }
 82             set { SetValue(PageSizeListProperty, value); }
 83         }
 84 
 85         /// <summary>转到首页</summary>
 86         public ICommand FirstPageCmd
 87         {
 88             get
 89             {
 90                 return new RelayCommand(() => { CurrentPage = 1; }
 91                                         , () => CurrentPage != 1);
 92             }
 93         }
 94 
 95         /// <summary>上一页</summary>
 96         public ICommand PreviousPageCmd
 97         {
 98             get { return new RelayCommand(() => { CurrentPage -= 1; }, () => CurrentPage > 1); }
 99         }
100 
101         /// <summary>下一页</summary>
102         public ICommand NextPageCmd
103         {
104             get { return new RelayCommand(() => { CurrentPage += 1; }, () => CurrentPage < TotalPages); }
105         }
106 
107         /// <summary>尾页</summary>
108         public ICommand LastPageCmd
109         {
110             get { return new RelayCommand(() => { CurrentPage = TotalPages; }, () => CurrentPage != TotalPages); }
111         }
112 
113         /// <summary>转到某一页</summary>
114         public ICommand TurnToPageCmd
115         {
116             get { return new RelayCommand(RaisePageRefreshEvent); }
117         }
118 
119         /// <summary>数据总大小</summary>
120         public uint ItemsCount
121         {
122             get { return (uint) GetValue(ItemsCountProperty); }
123             set { SetValue(ItemsCountProperty, value); }
124         }
125 
126         /// <summary>参数 选择Tuple(uint, uint)代表页数 和页大小,即index和length</summary>
127         public ICommand PageRefreshCommand
128         {
129             get { return (ICommand) GetValue(PageRefreshCommandProperty); }
130             set { SetValue(PageRefreshCommandProperty, value); }
131         }
132 
133         private static void TotalPagesPropertyChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
134         {
135             var ctrl = d as PaggingControl;
136             if (ctrl != null)
137             {
138                 if (ctrl.CurrentPage > ctrl.TotalPages)
139                 {
140                     ctrl.CurrentPage = ctrl.TotalPages;
141                 }
142                 else if (ctrl.CurrentPage <= 1)
143                 {
144                     ctrl.CurrentPage = 1;
145                 }
146                 ctrl.RaisePageRefreshEvent();
147             }
148         }
149 
150 
151         private static void CurrentPagePropertyChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
152         {
153             var ctrl = d as PaggingControl;
154             if (ctrl != null)
155             {
156                 if (ctrl.CurrentPage > ctrl.TotalPages)
157                 {
158                     ctrl.CurrentPage = ctrl.TotalPages;
159                 }
160                 else if (ctrl.CurrentPage <= 1)
161                 {
162                     ctrl.CurrentPage = 1;
163                 }
164                 ctrl.RaisePageRefreshEvent();
165             }
166         }
167 
168         private static void PageSizePropertyChangecallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
169         {
170             var ctrl = d as PaggingControl;
171             if (ctrl != null)
172             {
173                 ctrl.TotalPages = ctrl.ItemsCount/ctrl.PageSize + (ctrl.ItemsCount%ctrl.PageSize == 0 ? 0 : 1u);
174                 ctrl.RaisePageRefreshEvent();
175             }
176         }
177 
178         private static void ItemsCountPropertyChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
179         {
180             var ctrl = d as PaggingControl;
181             if (ctrl != null)
182             {
183                 ctrl.TotalPages = ctrl.ItemsCount/ctrl.PageSize + (ctrl.ItemsCount%ctrl.PageSize == 0 ? 0 : 1u);
184             }
185         }
186 
187         private void RaisePageRefreshEvent()
188         {
189             if (PageRefreshCommand != null)
190             {
191                 PageRefreshCommand.Execute(Tuple.Create(CurrentPage, PageSize));
192             }
193         }
194     }
195 }

2.效果图:

外部接口最少,只需要外部两个接口即可实现分页的所有功能。

3.参考网页:

 https://www.codeproject.com/Articles/350447/WPF-Paging-in-DataGrid-ListBox

将其代码进行了改进,忽略了不需要的部分,加入了一些简单的特性。更符合MVVM思想。

4.源码下载

https://files.cnblogs.com/files/chlm/PaggingDataGrid.rar

原文地址:https://www.cnblogs.com/congqiandehoulai/p/9003817.html