XAML:
12 3 64 5 7 20 218 199 1810 1712 14 16 22 4923 3724 3125 3026 28 2932 3633 3534 38 39 4840 41 42 43 44 45 46 47
后台代码:
Models:public class Pages { public string Name { get; set; } public int PageSize { get; set; } } public class User { public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } }
ViewMode:
class PageDataManager:INotifyPropertyChanged { private int number; public int Number { get { return number; } set { number = value; NotifyPropertyChanged("Number"); } } private int currentsize; public int Currentsize { get { return currentsize; } set { currentsize = value; NotifyPropertyChanged("Currentsize"); } } private int total; public int Total { get { return total; } set { total = value; NotifyPropertyChanged("Total"); } } private Listpages; public List Pages { get { return pages; } set { pages = value; NotifyPropertyChanged("Pages"); } } private List lst_user; public List Lst_user { get { return lst_user; } set { lst_user = value; NotifyPropertyChanged("Lst_user"); } } private List lst_bind; public List Lst_bind { get { return lst_bind; } set { lst_bind = value; NotifyPropertyChanged("Lst_bind"); } } //负责监视属性的变化 public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string Propertyname) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(Propertyname)); } } public PageDataManager() { this.Number = 50; this.Lst_user = new List (); for (int i = 0; i <= 1000; i++) { Lst_user.Add(new User() { Name="张三"+i.ToString(), Age=20, Address="中国河南" }); } //总页数=总数/每页显示的数 this.Total = Lst_user.Count()/Number; //初始化页数数组 this.Pages = new List (); for (int i = 1; i <= Total; i++) { this.Pages.Add(new Pages() { Name=i.ToString(), PageSize=i}); } this.Currentsize = 1; Pager(Currentsize); } //分页方法 public void Pager(int cize) { this.Currentsize = cize; this.Lst_bind = this.Lst_user.Take(this.Number * cize).Skip(this.Number * (cize - 1)).ToList(); } }
MainPage.cs
public partial class MainWindow : Window { PageDataManager data = new PageDataManager(); public MainWindow() { InitializeComponent(); this.DataContext = data; } private void Button_Click_1(object sender, RoutedEventArgs e) { data.Pager(((sender as Button).DataContext as Pages).PageSize); } }