Pagination.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WpfAppHotel
  8. {
  9. internal class Pagination
  10. {
  11. public event PropertyChangedEventHandler PropertyChanged;
  12. private static readonly int Countitems = 5;
  13. public int[] NPage { get; set; } = new int[Countitems];
  14. public string[] Visible { get; set; } = new string[Countitems];
  15. public string[] Gray { get; set; } = new string[Countitems];
  16. private int Countpages; //Количество страниц
  17. public int CountPages
  18. {
  19. get => Countpages;
  20. set
  21. {
  22. Countpages = value;
  23. for (int i = 1; i < Countitems; i++)
  24. {
  25. Visible[i] = CountPages <= i ? "Hidden" : "Visible";
  26. }
  27. }
  28. }
  29. private int Countpage; //Количество записей на странице
  30. public int CountPage
  31. {
  32. get => Countpage;
  33. set
  34. {
  35. Countpage = value;
  36. CountPages = CountList % value == 0 ? CountList / value : (CountList / value) + 1;
  37. }
  38. }
  39. private int Countlist; //Количество записей в списке
  40. public int CountList
  41. {
  42. get => Countlist;
  43. set
  44. {
  45. Countlist = value;
  46. CountPages = value % CountPage == 0 ? value / CountPage : 1 + (value / CountPage);
  47. }
  48. }
  49. private int Сurrentpage; //Текущая страница
  50. public int CurrentPage
  51. {
  52. get => Сurrentpage;
  53. set
  54. {
  55. Сurrentpage = value;
  56. if (Сurrentpage < 1)
  57. {
  58. Сurrentpage = 1;
  59. }
  60. if (Сurrentpage >= CountPages)
  61. {
  62. Сurrentpage = CountPages;
  63. }
  64. for (int i = 0; i < Countitems; i++)
  65. {
  66. if (Сurrentpage < (1 + (Countitems / 2)) || CountPages < Countitems)
  67. {
  68. NPage[i] = i + 1; //Eсли страница в начале списка
  69. }
  70. else if (Сurrentpage > CountPages - ((Countitems / 2) + 1))
  71. {
  72. NPage[i] = CountPages - (Countitems - 1) + i; //Eсли страница в конце списка
  73. }
  74. else
  75. {
  76. NPage[i] = Сurrentpage + i - (Countitems / 2); //Eсли страница в середине списка
  77. }
  78. }
  79. for (int i = 0; i < Countitems; i++)
  80. {
  81. Gray[i] = NPage[i] == Сurrentpage ? "Gray" : "Black";
  82. }
  83. PropertyChanged(this, new PropertyChangedEventArgs("NPage"));
  84. PropertyChanged(this, new PropertyChangedEventArgs("Visible"));
  85. PropertyChanged(this, new PropertyChangedEventArgs("Gray"));
  86. }
  87. }
  88. public Pagination()
  89. {
  90. for (int i = 0; i < Countitems; i++)
  91. {
  92. if (i == 0)
  93. {
  94. Visible[i] = "Visible";
  95. Gray[i] = "Gray";
  96. }
  97. else
  98. {
  99. Visible[i] = "Hidden";
  100. Gray[i] = "Black";
  101. }
  102. NPage[i] = i + 1;
  103. }
  104. Сurrentpage = 1;
  105. }
  106. }
  107. }