using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using GummingCommon; namespace Gumming { public class PagerControlViewModule: ViewModelBase { public PagerControlView View { get; set; } public delegate void PagerHandler(int currentpagenumber, int currentrows); public event PagerHandler OnPager; private ViewProperty _CurrentPageIndex = new ViewProperty(1); public int CurrentPageIndex { get { return _CurrentPageIndex.Value; } set { if (_CurrentPageIndex.Value != value) _CurrentPageIndex.Value = value; this.OnPropertyChanged("CurrentPageIndex"); } } private int _TotalPages = 1; public int TotalPages { get { return _TotalPages; } set { if (_TotalPages != value) _TotalPages = value; this.OnPropertyChanged("TotalPages"); } } private int records = 0; private int counts = 0; public ICommand FirstCommand { get; set; } public ICommand PreviousCommand { get; set; } public ICommand NextCommand { get; set; } public ICommand LastCommand { get; set; } private bool _FirstIsEnabled = false; public bool FirstIsEnabled { get { return _FirstIsEnabled; } set { if (_FirstIsEnabled != value) _FirstIsEnabled = value; this.OnPropertyChanged("FirstIsEnabled"); } } private bool _PreviousIsEnabled = false; public bool PreviousIsEnabled { get { return _PreviousIsEnabled; } set { if (_PreviousIsEnabled != value) _PreviousIsEnabled = value; this.OnPropertyChanged("PreviousIsEnabled"); } } private bool _NextIsEnabled = false; public bool NextIsEnabled { get { return _NextIsEnabled; } set { if (_NextIsEnabled != value) _NextIsEnabled = value; this.OnPropertyChanged("NextIsEnabled"); } } private bool _LastIsEnabled = false; public bool LastIsEnabled { get { return _LastIsEnabled; } set { if (_LastIsEnabled != value) _LastIsEnabled = value; this.OnPropertyChanged("LastIsEnabled"); } } private bool _PageIsEnabled = false; public bool PageIsEnabled { get { return _PageIsEnabled; } set { if (_PageIsEnabled != value) _PageIsEnabled = value; this.OnPropertyChanged("PageIsEnabled"); } } private string _PagerInfo1; public string PagerInfo1 { get { return _PagerInfo1; } set { if (_PagerInfo1 != value) _PagerInfo1 = value; this.OnPropertyChanged("PagerInfo1"); } } private string _TotalInfo; public string TotalInfo { get { return _TotalInfo; } set { if (_TotalInfo != value) _TotalInfo = value; this.OnPropertyChanged("TotalInfo"); } } public IEnumerable PagerRows { get; set; } private ViewProperty _CurrentPagerRows = new ViewProperty(); public int CurrentPagerRows { get { return _CurrentPagerRows.Value; } set { if (_CurrentPagerRows.Value == value) return; _CurrentPagerRows.Value = value; OnPropertyChanged("CurrentPagerRows"); } } public PagerControlViewModule() { View = new PagerControlView(); View.DataContext = this; PageIsEnabled = false; IList pagerNumbers = new List() { 10, 20, 50, 100 }; PagerRows = pagerNumbers; FirstCommand = new DelegateCommand(OnFirstCommand); PreviousCommand = new DelegateCommand(OnPreviousCommand); NextCommand = new DelegateCommand(OnNextCommand); LastCommand = new DelegateCommand(OnLastCommand); _CurrentPagerRows.ValueChanged += new Action(_CurrentPagerRows_ValueChanged); _CurrentPageIndex.ValueChanged += new Action(_CurrentPageIndex_ValueChanged); } private void _CurrentPageIndex_ValueChanged() { if (OnPager != null) { OnPager(CurrentPageIndex, CurrentPagerRows); } } private void _CurrentPagerRows_ValueChanged() { if (CurrentPageIndex == 1) { if (OnPager != null) { OnPager(CurrentPageIndex, CurrentPagerRows); } } else { CurrentPageIndex = 1; } } public void SetPager(int _rows, int _pagenumber, int _pagecount, int _counts , int _records) { CurrentPagerRows = _rows; if (!PagerRows.Contains(_rows)) { IList pagerNumbers = PagerRows.ToList(); pagerNumbers.Add(_rows); PagerRows = pagerNumbers; OnPropertyChanged("PagerRows"); } CurrentPageIndex = _pagenumber; TotalPages = _pagecount; records = _records; counts = _counts; SetStatus(); PageIsEnabled = true; } private void OnFirstCommand(object sender) { CurrentPageIndex = 1; SetStatus(); } private void OnPreviousCommand(object sender) { CurrentPageIndex = CurrentPageIndex - 1; SetStatus(); } private void OnNextCommand(object sender) { CurrentPageIndex = CurrentPageIndex + 1; SetStatus(); } private void OnLastCommand(object sender) { CurrentPageIndex = TotalPages; SetStatus(); } private void SetStatus() { if (CurrentPageIndex <= 1) { FirstIsEnabled = false; PreviousIsEnabled = false; } else { FirstIsEnabled = true; PreviousIsEnabled = true; } if (CurrentPageIndex >= TotalPages) { NextIsEnabled = false; LastIsEnabled = false; } else { NextIsEnabled = true; LastIsEnabled = true; } TotalInfo = string.Format("当前页面记录{0}/共{1}条记录", counts, records); } } }