1.主要代碼:ide
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.效果圖:ui
外部接口最少,只須要外部兩個接口便可實現分頁的全部功能。spa
3.參考網頁:code
https://www.codeproject.com/Articles/350447/WPF-Paging-in-DataGrid-ListBoxxml
將其代碼進行了改進,忽略了不須要的部分,加入了一些簡單的特性。更符合MVVM思想。blog
4.源碼下載接口
https://files.cnblogs.com/files/chlm/PaggingDataGrid.rar資源