MuchinfoPager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. using Muchinfo.DataPager.Base;
  2. using Muchinfo.DataPager.Extensions;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. namespace Muchinfo.DataPager
  9. {
  10. public class MuchinfoPager : Control, ICommandSource
  11. {
  12. #region Fields
  13. private bool? _initializingData = null;
  14. private bool _areHandlersSuspended;
  15. public static readonly DependencyProperty PageIndexProperty;
  16. public static readonly DependencyProperty PageSizeProperty;
  17. public static readonly DependencyProperty ItemCountProperty;
  18. private static readonly DependencyPropertyKey PageCountPropertyKey;
  19. public static readonly DependencyProperty PageCountProperty;
  20. private static readonly DependencyPropertyKey CanMoveToFirstPagePropertyKey;
  21. public static readonly DependencyProperty CanMoveToFirstPageProperty;
  22. private static readonly DependencyPropertyKey CanMoveToLastPagePropertyKey;
  23. public static readonly DependencyProperty CanMoveToLastPageProperty;
  24. private static readonly DependencyPropertyKey CanMoveToNextPagePropertyKey;
  25. public static readonly DependencyProperty CanMoveToNextPageProperty;
  26. private static readonly DependencyPropertyKey CanChangePagePropertyKey;
  27. public static readonly DependencyProperty CanChangePageProperty;
  28. private static readonly DependencyPropertyKey CanMoveToPreviousPagePropertyKey;
  29. public static readonly DependencyProperty CanMoveToPreviousPageProperty;
  30. public static readonly DependencyProperty NumericButtonCountProperty;
  31. public static readonly DependencyProperty NumericButtonStyleProperty;
  32. #endregion
  33. #region Properties
  34. public int PageIndex
  35. {
  36. get
  37. {
  38. return (int)base.GetValue(MuchinfoPager.PageIndexProperty);
  39. }
  40. set
  41. {
  42. base.SetValue(MuchinfoPager.PageIndexProperty, value);
  43. }
  44. }
  45. public int PageSize
  46. {
  47. get
  48. {
  49. return (int)base.GetValue(MuchinfoPager.PageSizeProperty);
  50. }
  51. set
  52. {
  53. base.SetValue(MuchinfoPager.PageSizeProperty, value);
  54. }
  55. }
  56. public int ItemCount
  57. {
  58. get
  59. {
  60. return (int)base.GetValue(MuchinfoPager.ItemCountProperty);
  61. }
  62. set
  63. {
  64. base.SetValue(MuchinfoPager.ItemCountProperty, value);
  65. }
  66. }
  67. public int PageCount
  68. {
  69. get
  70. {
  71. return (int)base.GetValue(MuchinfoPager.PageCountProperty);
  72. }
  73. private set
  74. {
  75. base.SetValue(MuchinfoPager.PageCountPropertyKey, value);
  76. }
  77. }
  78. public bool CanMoveToFirstPage
  79. {
  80. get
  81. {
  82. return (bool)base.GetValue(MuchinfoPager.CanMoveToFirstPageProperty);
  83. }
  84. private set
  85. {
  86. base.SetValue(MuchinfoPager.CanMoveToFirstPagePropertyKey, value);
  87. }
  88. }
  89. public bool CanMoveToLastPage
  90. {
  91. get
  92. {
  93. return (bool)base.GetValue(MuchinfoPager.CanMoveToLastPageProperty);
  94. }
  95. private set
  96. {
  97. base.SetValue(MuchinfoPager.CanMoveToLastPagePropertyKey, value);
  98. }
  99. }
  100. public bool CanMoveToNextPage
  101. {
  102. get
  103. {
  104. return (bool)base.GetValue(MuchinfoPager.CanMoveToNextPageProperty);
  105. }
  106. private set
  107. {
  108. base.SetValue(MuchinfoPager.CanMoveToNextPagePropertyKey, value);
  109. }
  110. }
  111. public bool CanChangePage
  112. {
  113. get
  114. {
  115. return (bool)base.GetValue(MuchinfoPager.CanChangePageProperty);
  116. }
  117. private set
  118. {
  119. base.SetValue(MuchinfoPager.CanChangePagePropertyKey, value);
  120. }
  121. }
  122. public bool CanMoveToPreviousPage
  123. {
  124. get
  125. {
  126. return (bool)base.GetValue(MuchinfoPager.CanMoveToPreviousPageProperty);
  127. }
  128. private set
  129. {
  130. base.SetValue(MuchinfoPager.CanMoveToPreviousPagePropertyKey, value);
  131. }
  132. }
  133. public int NumericButtonCount
  134. {
  135. get
  136. {
  137. return (int)base.GetValue(MuchinfoPager.NumericButtonCountProperty);
  138. }
  139. set
  140. {
  141. base.SetValue(MuchinfoPager.NumericButtonCountProperty, value);
  142. }
  143. }
  144. public Style NumericButtonStyle
  145. {
  146. get
  147. {
  148. return (Style)base.GetValue(MuchinfoPager.NumericButtonStyleProperty);
  149. }
  150. set
  151. {
  152. base.SetValue(MuchinfoPager.NumericButtonStyleProperty, value);
  153. }
  154. }
  155. private bool IsInitializingData
  156. {
  157. get
  158. {
  159. return this._initializingData.HasValue && this._initializingData.Value;
  160. }
  161. }
  162. #endregion
  163. #region Command Handle
  164. private static void RegisterCommand(ICommand command, ExecutedRoutedEventHandler executed, CanExecuteRoutedEventHandler canExecute)
  165. {
  166. CommandManager.RegisterClassCommandBinding(typeof(MuchinfoPager), new CommandBinding(command, executed, canExecute));
  167. }
  168. private static void RegisterCommands()
  169. {
  170. MuchinfoPager.RegisterCommand(MuchinfoPagerCommands.MoveToFirstPage, new ExecutedRoutedEventHandler(MuchinfoPager.OnMoveToFirstPage), new CanExecuteRoutedEventHandler(MuchinfoPager.CanMoveToFirstPageExecute));
  171. MuchinfoPager.RegisterCommand(MuchinfoPagerCommands.MoveToLastPage, new ExecutedRoutedEventHandler(MuchinfoPager.OnMoveToLastPage), new CanExecuteRoutedEventHandler(MuchinfoPager.CanMoveToLastPageExecute));
  172. MuchinfoPager.RegisterCommand(MuchinfoPagerCommands.MoveToNextPage, new ExecutedRoutedEventHandler(MuchinfoPager.OnMoveToNextPage), new CanExecuteRoutedEventHandler(MuchinfoPager.CanMoveToNextPageExecute));
  173. MuchinfoPager.RegisterCommand(MuchinfoPagerCommands.MoveToPage, new ExecutedRoutedEventHandler(MuchinfoPager.OnMoveToPage), new CanExecuteRoutedEventHandler(MuchinfoPager.CanMoveToPageExecute));
  174. MuchinfoPager.RegisterCommand(MuchinfoPagerCommands.MoveToPreviousPage, new ExecutedRoutedEventHandler(MuchinfoPager.OnMoveToPreviousPage), new CanExecuteRoutedEventHandler(MuchinfoPager.CanMoveToPreviousPageExecute));
  175. }
  176. private static void OnMoveToFirstPage(object sender, ExecutedRoutedEventArgs e)
  177. {
  178. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  179. if (MuchinfoDataPager != null)
  180. {
  181. MuchinfoDataPager.MoveToFirstPage();
  182. }
  183. }
  184. private static void CanMoveToFirstPageExecute(object sender, CanExecuteRoutedEventArgs e)
  185. {
  186. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  187. if (MuchinfoDataPager != null)
  188. {
  189. e.CanExecute = MuchinfoDataPager.CanMoveToFirstPage;
  190. }
  191. }
  192. private static void OnMoveToLastPage(object sender, ExecutedRoutedEventArgs e)
  193. {
  194. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  195. if (MuchinfoDataPager != null)
  196. {
  197. MuchinfoDataPager.MoveToLastPage();
  198. }
  199. }
  200. private static void CanMoveToLastPageExecute(object sender, CanExecuteRoutedEventArgs e)
  201. {
  202. e.CanExecute = false;
  203. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  204. if (MuchinfoDataPager != null)
  205. {
  206. e.CanExecute = MuchinfoDataPager.CanMoveToLastPage;
  207. }
  208. }
  209. private static void OnMoveToNextPage(object sender, ExecutedRoutedEventArgs e)
  210. {
  211. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  212. if (MuchinfoDataPager != null)
  213. {
  214. MuchinfoDataPager.MoveToNextPage();
  215. }
  216. }
  217. private static void CanMoveToNextPageExecute(object sender, CanExecuteRoutedEventArgs e)
  218. {
  219. e.CanExecute = false;
  220. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  221. if (MuchinfoDataPager != null)
  222. {
  223. e.CanExecute = MuchinfoDataPager.CanMoveToNextPage;
  224. }
  225. }
  226. private static void OnMoveToPage(object sender, ExecutedRoutedEventArgs e)
  227. {
  228. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  229. if (MuchinfoDataPager != null)
  230. {
  231. if (!(e.Parameter is int))
  232. {
  233. throw new ArgumentException("The parameter of the MoveToPage command should be an integer.");
  234. }
  235. MuchinfoDataPager.MoveToPage((int)e.Parameter);
  236. }
  237. }
  238. private static void CanMoveToPageExecute(object sender, CanExecuteRoutedEventArgs e)
  239. {
  240. e.CanExecute = false;
  241. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  242. if (MuchinfoDataPager != null)
  243. {
  244. e.CanExecute = MuchinfoDataPager.CanChangePage;
  245. }
  246. }
  247. private static void OnMoveToPreviousPage(object sender, ExecutedRoutedEventArgs e)
  248. {
  249. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  250. if (MuchinfoDataPager != null)
  251. {
  252. MuchinfoDataPager.MoveToPreviousPage();
  253. }
  254. }
  255. private static void CanMoveToPreviousPageExecute(object sender, CanExecuteRoutedEventArgs e)
  256. {
  257. e.CanExecute = false;
  258. MuchinfoPager MuchinfoDataPager = sender as MuchinfoPager;
  259. if (MuchinfoDataPager != null)
  260. {
  261. e.CanExecute = MuchinfoDataPager.CanMoveToPreviousPage;
  262. }
  263. }
  264. #endregion
  265. #region Constructor
  266. static MuchinfoPager()
  267. {
  268. MuchinfoPager.PageIndexProperty = DependencyPropertyExtensions.Register("PageIndex", typeof(int), typeof(MuchinfoPager), new PropertyMetadata(-1, new PropertyChangedCallback(MuchinfoPager.OnPageIndexPropertyChanged)), new ValidateValueCallback(MuchinfoPager.IsValidDataPagerIndex));
  269. MuchinfoPager.PageSizeProperty = DependencyProperty.Register("PageSize", typeof(int), typeof(MuchinfoPager), new PropertyMetadata(20, new PropertyChangedCallback(MuchinfoPager.OnPageSizePropertyChanged)));
  270. MuchinfoPager.ItemCountProperty = DependencyProperty.Register("ItemCount", typeof(int), typeof(MuchinfoPager), new PropertyMetadata(new PropertyChangedCallback(MuchinfoPager.OnItemCountPropertyChanged)));
  271. MuchinfoPager.PageCountPropertyKey = DependencyPropertyExtensions.RegisterReadOnly("PageCount", typeof(int), typeof(MuchinfoPager), new PropertyMetadata());
  272. MuchinfoPager.PageCountProperty = MuchinfoPager.PageCountPropertyKey.DependencyProperty;
  273. MuchinfoPager.CanMoveToFirstPagePropertyKey = DependencyPropertyExtensions.RegisterReadOnly("CanMoveToFirstPage", typeof(bool), typeof(MuchinfoPager), new PropertyMetadata());
  274. MuchinfoPager.CanMoveToFirstPageProperty = MuchinfoPager.CanMoveToFirstPagePropertyKey.DependencyProperty;
  275. MuchinfoPager.CanMoveToLastPagePropertyKey = DependencyPropertyExtensions.RegisterReadOnly("CanMoveToLastPage", typeof(bool), typeof(MuchinfoPager), new PropertyMetadata());
  276. MuchinfoPager.CanMoveToLastPageProperty = MuchinfoPager.CanMoveToLastPagePropertyKey.DependencyProperty;
  277. MuchinfoPager.CanMoveToNextPagePropertyKey = DependencyPropertyExtensions.RegisterReadOnly("CanMoveToNextPage", typeof(bool), typeof(MuchinfoPager), new PropertyMetadata());
  278. MuchinfoPager.CanMoveToNextPageProperty = MuchinfoPager.CanMoveToNextPagePropertyKey.DependencyProperty;
  279. MuchinfoPager.CanChangePagePropertyKey = DependencyPropertyExtensions.RegisterReadOnly("CanChangePage", typeof(bool), typeof(MuchinfoPager), new PropertyMetadata());
  280. MuchinfoPager.CanChangePageProperty = MuchinfoPager.CanChangePagePropertyKey.DependencyProperty;
  281. MuchinfoPager.CanMoveToPreviousPagePropertyKey = DependencyPropertyExtensions.RegisterReadOnly("CanMoveToPreviousPage", typeof(bool), typeof(MuchinfoPager), new PropertyMetadata());
  282. MuchinfoPager.CanMoveToPreviousPageProperty = MuchinfoPager.CanMoveToPreviousPagePropertyKey.DependencyProperty;
  283. MuchinfoPager.NumericButtonCountProperty = DependencyProperty.Register("NumericButtonCount", typeof(int), typeof(MuchinfoPager), new PropertyMetadata(10));
  284. MuchinfoPager.NumericButtonStyleProperty = DependencyProperty.Register("NumericButtonStyle", typeof(Style), typeof(MuchinfoPager), new PropertyMetadata(null));
  285. MuchinfoPager.RegisterCommands();
  286. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MuchinfoPager), new FrameworkPropertyMetadata(typeof(MuchinfoPager)));
  287. }
  288. public MuchinfoPager()
  289. {
  290. this.Loaded += MuchinfoPager_Loaded;
  291. }
  292. #endregion
  293. #region Methods
  294. private static void OnPageIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  295. {
  296. var pager = (MuchinfoPager)d;
  297. if (pager.IsInitializingData)
  298. {
  299. return;
  300. }
  301. if (pager._areHandlersSuspended)
  302. {
  303. return;
  304. }
  305. pager.UpdateReadonlyProperties();
  306. pager.ExecuteCommand();
  307. }
  308. private void CheckPageIndexChange(int oldPageIndex, int newPageIndex)
  309. {
  310. if (this.PageSize == 0 && newPageIndex != -1)
  311. {
  312. this.SetValueNoCallback(MuchinfoPager.PageIndexProperty, oldPageIndex);
  313. throw new ArgumentOutOfRangeException("newPageIndex", "PageIndex can only be set to -1 when the PageSize is 0.");
  314. }
  315. if (this.PageSize != 0 && newPageIndex == -1)
  316. {
  317. this.SetValueNoCallback(MuchinfoPager.PageIndexProperty, oldPageIndex);
  318. throw new ArgumentOutOfRangeException("newPageIndex", "PageIndex cannot be negative when PageSize is positive.");
  319. }
  320. }
  321. private static bool IsValidDataPagerIndex(object value)
  322. {
  323. return (int)value >= -1;
  324. }
  325. private static void OnPageSizePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  326. {
  327. var pager = (MuchinfoPager)d;
  328. if (pager.IsInitializingData)
  329. {
  330. return;
  331. }
  332. pager.UpdatePageCount();
  333. pager.UpdateReadonlyProperties();
  334. pager.ExecuteCommand();
  335. }
  336. private static void OnItemCountPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  337. {
  338. var pager = (MuchinfoPager)d;
  339. if (pager.IsInitializingData)
  340. {
  341. return;
  342. }
  343. pager.UpdatePageCount();
  344. pager.UpdateReadonlyProperties();
  345. //此处调用Command会触发两次查询, 因为PageIndex改变也会触发
  346. //pager.ExecuteCommand();
  347. }
  348. public bool MoveToFirstPage()
  349. {
  350. if (PageCount > 0) PageIndex = 0;
  351. return true;
  352. }
  353. public bool MoveToLastPage()
  354. {
  355. if (PageCount > 0) PageIndex = PageCount - 1;
  356. return true;
  357. }
  358. public bool MoveToNextPage()
  359. {
  360. if (PageCount > 0 && PageIndex < PageCount - 1) PageIndex = PageIndex + 1;
  361. return true;
  362. }
  363. public bool MoveToPage(int pageIndex)
  364. {
  365. if (pageIndex < 0)
  366. {
  367. PageIndex = 0;
  368. }
  369. else if (pageIndex > PageCount)
  370. {
  371. PageIndex = PageCount - 1;
  372. }
  373. else
  374. {
  375. PageIndex = pageIndex;
  376. }
  377. return true;
  378. }
  379. public bool MoveToPreviousPage()
  380. {
  381. if (PageCount > 0 && PageIndex > 0) PageIndex = PageIndex - 1;
  382. return true;
  383. }
  384. private void SetValueNoCallback(DependencyProperty property, object value)
  385. {
  386. this._areHandlersSuspended = true;
  387. try
  388. {
  389. base.SetValue(property, value);
  390. }
  391. finally
  392. {
  393. this._areHandlersSuspended = false;
  394. }
  395. }
  396. private void UpdatePageCount()
  397. {
  398. if (ItemCount > 0 && PageIndex < 0) PageIndex = 0;
  399. this.PageCount = Math.Max(1, (int)Math.Ceiling((double)ItemCount / (double)PageSize));
  400. }
  401. private void UpdateReadonlyProperties()
  402. {
  403. var canChangePage = ItemCount > 0;
  404. CanChangePage = canChangePage;
  405. CanMoveToFirstPage = canChangePage && this.PageIndex > 0;
  406. CanMoveToLastPage = canChangePage && this.PageIndex < this.PageCount - 1;
  407. CanMoveToPreviousPage = canChangePage && this.PageIndex > 0;
  408. CanMoveToNextPage = canChangePage && this.PageIndex < this.PageCount - 1;
  409. CommandManager.InvalidateRequerySuggested();
  410. }
  411. #endregion
  412. #region Override Handle
  413. public override void BeginInit()
  414. {
  415. base.BeginInit();
  416. this.BeginInitInternal();
  417. }
  418. //public override void EndInit()
  419. //{
  420. // this.EndInitInternal();
  421. // base.EndInit();
  422. //}
  423. private void MuchinfoPager_Loaded(object sender, RoutedEventArgs e)
  424. {
  425. //使用loaded代替EndInit,界面绑定值时会触发查询
  426. this.EndInitInternal();
  427. }
  428. private void BeginInitInternal()
  429. {
  430. this._initializingData = new bool?(true);
  431. }
  432. private void EndInitInternal()
  433. {
  434. this._initializingData = new bool?(false);
  435. UpdatePageCount();
  436. UpdateReadonlyProperties();
  437. }
  438. #endregion
  439. #region ICommand
  440. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(MuchinfoPager), new PropertyMetadata(null));
  441. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(MuchinfoPager), new PropertyMetadata(null));
  442. public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(UIElement), typeof(MuchinfoPager), null);
  443. [TypeConverter(typeof(CommandConverter))]
  444. public ICommand Command
  445. {
  446. get
  447. {
  448. return (ICommand)base.GetValue(MuchinfoPager.CommandProperty);
  449. }
  450. set
  451. {
  452. base.SetValue(MuchinfoPager.CommandProperty, value);
  453. }
  454. }
  455. public object CommandParameter
  456. {
  457. get
  458. {
  459. return base.GetValue(MuchinfoPager.CommandParameterProperty);
  460. }
  461. set
  462. {
  463. base.SetValue(MuchinfoPager.CommandParameterProperty, value);
  464. }
  465. }
  466. public IInputElement CommandTarget
  467. {
  468. get { return null; }
  469. }
  470. private void ExecuteCommand()
  471. {
  472. if (PageIndex < 0) return;
  473. if (this.Command != null)
  474. {
  475. var routedCommand = this.Command as RoutedCommand;
  476. if (routedCommand == null)
  477. {
  478. this.Command.Execute(this.CommandParameter);
  479. return;
  480. }
  481. routedCommand.Execute(this.CommandParameter, this.CommandTarget ?? this);
  482. }
  483. }
  484. #endregion
  485. }
  486. }