PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/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
 17using System;
 18using System.Collections.Generic;
 19using System.Linq;
 20using System.Text;
 21using System.Windows;
 22using System.Windows.Controls;
 23using System.Windows.Data;
 24using System.Windows.Documents;
 25using System.Windows.Input;
 26using System.Windows.Media;
 27using System.Windows.Media.Imaging;
 28using System.Windows.Navigation;
 29using System.Windows.Shapes;
 30using System.Collections.ObjectModel;
 31
 32namespace CSWPFPaging
 33{
 34    /// <summary>
 35    /// Interaction logic for MainWindow.xaml
 36    /// </summary>
 37    public partial class MainWindow : Window
 38    {
 39        public MainWindow()
 40        {
 41            InitializeComponent();
 42        }
 43
 44        CollectionViewSource view = new CollectionViewSource();
 45        ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
 46        int currentPageIndex = 0;
 47        int itemPerPage = 20;
 48        int totalPage = 0;
 49
 50        private void ShowCurrentPageIndex()
 51        {
 52            this.tbCurrentPage.Text = (currentPageIndex + 1).ToString();
 53        }
 54        private void Window_Loaded(object sender, RoutedEventArgs e)
 55        {
 56            int itemcount = 107;
 57            for (int j = 0; j < itemcount; j++)
 58            {
 59                customers.Add(new Customer()
 60                {
 61                    ID = j,
 62                    Name = "item" + j.ToString(),
 63                    Age = 10 + j,
 64                    Country = "China"
 65                });
 66            }
 67
 68            // Calculate the total pages
 69            totalPage = itemcount / itemPerPage;
 70            if (itemcount % itemPerPage != 0)
 71            {
 72                totalPage += 1;
 73            }
 74
 75            view.Source = customers;
 76
 77            view.Filter += new FilterEventHandler(view_Filter);
 78
 79            this.listView1.DataContext = view;
 80            ShowCurrentPageIndex();
 81            this.tbTotalPage.Text = totalPage.ToString();
 82        }
 83
 84        void view_Filter(object sender, FilterEventArgs e)
 85        {
 86            int index = customers.IndexOf((Customer)e.Item);
 87
 88            if (index >= itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex + 1))
 89            {
 90                e.Accepted = true;
 91            }
 92            else
 93            {
 94                e.Accepted = false;
 95            }
 96        }
 97
 98        private void btnFirst_Click(object sender, RoutedEventArgs e)
 99        {
100            // Display the first page
101            if (currentPageIndex != 0)
102            {
103                currentPageIndex = 0;
104                view.View.Refresh();
105            }
106            ShowCurrentPageIndex();
107        }
108
109        private void btnPrev_Click(object sender, RoutedEventArgs e)
110        {
111            // Display previous page
112            if (currentPageIndex > 0)
113            {
114                currentPageIndex--;
115                view.View.Refresh();
116            }
117            ShowCurrentPageIndex();
118        }
119
120        private void btnNext_Click(object sender, RoutedEventArgs e)
121        {
122            // Display next page
123            if (currentPageIndex < totalPage - 1)
124            {
125                currentPageIndex++;
126                view.View.Refresh();
127            }
128            ShowCurrentPageIndex();
129        }
130
131        private void btnLast_Click(object sender, RoutedEventArgs e)
132        {
133            // Display the last page
134            if (currentPageIndex != totalPage - 1)
135            {
136                currentPageIndex = totalPage - 1;
137                view.View.Refresh();
138            }
139            ShowCurrentPageIndex();
140        }
141    }
142}