/Visual Studio 2008/CSWPFPaging/MainWindow.xaml.cs
C# | 142 lines | 105 code | 14 blank | 23 comment | 11 complexity | 8feca5bc518833b1a57db78c36610473 MD5 | raw file
- /************************************* Module Header **************************************\
- * Module Name: MainWindow.xaml.cs
- * Project: CSWPFPaging
- * Copyright (c) Microsoft Corporation.
- *
- * The sample demonstrates how to page data in WPF.
- *
- *
- * This source is subject to the Microsoft Public License.
- * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
- * All other rights reserved.
- *
- * History:
- * * 12/02/2009 3:00 PM Zhi-Xin Ye Created
- */
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Collections.ObjectModel;
-
- namespace CSWPFPaging
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
-
- CollectionViewSource view = new CollectionViewSource();
- ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
- int currentPageIndex = 0;
- int itemPerPage = 20;
- int totalPage = 0;
-
- private void ShowCurrentPageIndex()
- {
- this.tbCurrentPage.Text = (currentPageIndex + 1).ToString();
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- int itemcount = 107;
- for (int j = 0; j < itemcount; j++)
- {
- customers.Add(new Customer()
- {
- ID = j,
- Name = "item" + j.ToString(),
- Age = 10 + j,
- Country = "China"
- });
- }
-
- // Calculate the total pages
- totalPage = itemcount / itemPerPage;
- if (itemcount % itemPerPage != 0)
- {
- totalPage += 1;
- }
-
- view.Source = customers;
-
- view.Filter += new FilterEventHandler(view_Filter);
-
- this.listView1.DataContext = view;
- ShowCurrentPageIndex();
- this.tbTotalPage.Text = totalPage.ToString();
- }
-
- void view_Filter(object sender, FilterEventArgs e)
- {
- int index = customers.IndexOf((Customer)e.Item);
-
- if (index >= itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex + 1))
- {
- e.Accepted = true;
- }
- else
- {
- e.Accepted = false;
- }
- }
-
- private void btnFirst_Click(object sender, RoutedEventArgs e)
- {
- // Display the first page
- if (currentPageIndex != 0)
- {
- currentPageIndex = 0;
- view.View.Refresh();
- }
- ShowCurrentPageIndex();
- }
-
- private void btnPrev_Click(object sender, RoutedEventArgs e)
- {
- // Display previous page
- if (currentPageIndex > 0)
- {
- currentPageIndex--;
- view.View.Refresh();
- }
- ShowCurrentPageIndex();
- }
-
- private void btnNext_Click(object sender, RoutedEventArgs e)
- {
- // Display next page
- if (currentPageIndex < totalPage - 1)
- {
- currentPageIndex++;
- view.View.Refresh();
- }
- ShowCurrentPageIndex();
- }
-
- private void btnLast_Click(object sender, RoutedEventArgs e)
- {
- // Display the last page
- if (currentPageIndex != totalPage - 1)
- {
- currentPageIndex = totalPage - 1;
- view.View.Refresh();
- }
- ShowCurrentPageIndex();
- }
- }
- }