PageRenderTime 60ms CodeModel.GetById 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSWPFPaging/MainWindow.xaml.cs

#
C# | 142 lines | 105 code | 14 blank | 23 comment | 11 complexity | 8feca5bc518833b1a57db78c36610473 MD5 | raw file
  1. /************************************* Module Header **************************************\
  2. * Module Name: MainWindow.xaml.cs
  3. * Project: CSWPFPaging
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The sample demonstrates how to page data in WPF.
  7. *
  8. *
  9. * This source is subject to the Microsoft Public License.
  10. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. * All other rights reserved.
  12. *
  13. * History:
  14. * * 12/02/2009 3:00 PM Zhi-Xin Ye Created
  15. */
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Data;
  23. using System.Windows.Documents;
  24. using System.Windows.Input;
  25. using System.Windows.Media;
  26. using System.Windows.Media.Imaging;
  27. using System.Windows.Navigation;
  28. using System.Windows.Shapes;
  29. using System.Collections.ObjectModel;
  30. namespace CSWPFPaging
  31. {
  32. /// <summary>
  33. /// Interaction logic for MainWindow.xaml
  34. /// </summary>
  35. public partial class MainWindow : Window
  36. {
  37. public MainWindow()
  38. {
  39. InitializeComponent();
  40. }
  41. CollectionViewSource view = new CollectionViewSource();
  42. ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
  43. int currentPageIndex = 0;
  44. int itemPerPage = 20;
  45. int totalPage = 0;
  46. private void ShowCurrentPageIndex()
  47. {
  48. this.tbCurrentPage.Text = (currentPageIndex + 1).ToString();
  49. }
  50. private void Window_Loaded(object sender, RoutedEventArgs e)
  51. {
  52. int itemcount = 107;
  53. for (int j = 0; j < itemcount; j++)
  54. {
  55. customers.Add(new Customer()
  56. {
  57. ID = j,
  58. Name = "item" + j.ToString(),
  59. Age = 10 + j,
  60. Country = "China"
  61. });
  62. }
  63. // Calculate the total pages
  64. totalPage = itemcount / itemPerPage;
  65. if (itemcount % itemPerPage != 0)
  66. {
  67. totalPage += 1;
  68. }
  69. view.Source = customers;
  70. view.Filter += new FilterEventHandler(view_Filter);
  71. this.listView1.DataContext = view;
  72. ShowCurrentPageIndex();
  73. this.tbTotalPage.Text = totalPage.ToString();
  74. }
  75. void view_Filter(object sender, FilterEventArgs e)
  76. {
  77. int index = customers.IndexOf((Customer)e.Item);
  78. if (index >= itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex + 1))
  79. {
  80. e.Accepted = true;
  81. }
  82. else
  83. {
  84. e.Accepted = false;
  85. }
  86. }
  87. private void btnFirst_Click(object sender, RoutedEventArgs e)
  88. {
  89. // Display the first page
  90. if (currentPageIndex != 0)
  91. {
  92. currentPageIndex = 0;
  93. view.View.Refresh();
  94. }
  95. ShowCurrentPageIndex();
  96. }
  97. private void btnPrev_Click(object sender, RoutedEventArgs e)
  98. {
  99. // Display previous page
  100. if (currentPageIndex > 0)
  101. {
  102. currentPageIndex--;
  103. view.View.Refresh();
  104. }
  105. ShowCurrentPageIndex();
  106. }
  107. private void btnNext_Click(object sender, RoutedEventArgs e)
  108. {
  109. // Display next page
  110. if (currentPageIndex < totalPage - 1)
  111. {
  112. currentPageIndex++;
  113. view.View.Refresh();
  114. }
  115. ShowCurrentPageIndex();
  116. }
  117. private void btnLast_Click(object sender, RoutedEventArgs e)
  118. {
  119. // Display the last page
  120. if (currentPageIndex != totalPage - 1)
  121. {
  122. currentPageIndex = totalPage - 1;
  123. view.View.Refresh();
  124. }
  125. ShowCurrentPageIndex();
  126. }
  127. }
  128. }