123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- 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;
- namespace AIRS.usercontrol
- {
- /// <summary>
- /// 分页控件
- /// </summary>
- public partial class PageNumControl: UserControl, INotifyPropertyChanged
- {
- #region 事件
- /// <summary>
- /// 分页事件
- /// </summary>
- public event EventHandler<PageChangedEventArgs> PageChanged;
- #endregion
- #region 变量
- private ObservableCollection<PageControlItemModel> _collection = new ObservableCollection<PageControlItemModel>();
- private List<PageControlItemModel> _list = null;
- #endregion
- #region 属性
- private int _FontSize = 36;
- /// <summary>
- /// 文字字体大小
- /// </summary>
- public int FontSize
- {
- get { return _FontSize; }
- set
- {
- _FontSize = value;
- OnPropertyChanged("FontSize");
- CalcPageNumList(); //计算页码
- }
- }
- #endregion
- #region 分页相关属性
- private int _PageCount = 1;
- /// <summary>
- /// 总页数
- /// </summary>
- public int PageCount
- {
- get { return _PageCount; }
- set
- {
- _PageCount = value;
- OnPropertyChanged("PageCount");
- }
- }
- private int _Page = 1;
- /// <summary>
- /// 当前页码
- /// </summary>
- public int Page
- {
- get { return _Page; }
- set
- {
- _Page = value;
- OnPropertyChanged("Page");
- CalcPageNumList(); //计算页码
- }
- }
- private int _RecordCount = 0;
- /// <summary>
- /// 记录总数
- /// </summary>
- public int RecordCount
- {
- get { return _RecordCount; }
- set
- {
- _RecordCount = value;
- OnPropertyChanged("RecordCount");
- CalcPageNumList(); //计算页码
- }
- }
- private int _PageSize = 10;
- /// <summary>
- /// 每页记录数
- /// </summary>
- public int PageSize
- {
- get { return _PageSize; }
- set
- {
- _PageSize = value;
- OnPropertyChanged("PageSize");
- CalcPageNumList(); //计算页码
- }
- }
- private int _ContinuousCount = 3;
- /// <summary>
- /// 当前页码右边连续页码数
- /// </summary>
- public int ContinuousCount
- {
- get { return _ContinuousCount; }
- set
- {
- _ContinuousCount = value;
- OnPropertyChanged("_ContinuousCount");
- CalcPageNumList(); //计算页码
- }
- }
- #endregion
- #region 构造函数
- public PageNumControl()
- {
- InitializeComponent();
- }
- #endregion
- #region 单击页码事件
- private void btnNum_Click(object sender, RoutedEventArgs e)
- {
- if (PageChanged != null)
- {
- Button btn = sender as Button;
- PageControlItemModel itemModel = btn.CommandParameter as PageControlItemModel;
- if (itemModel.Page != Page)
- {
- Page = itemModel.Page;
- CalcPageNumList();
- PageChangedEventArgs args = new PageChangedEventArgs(itemModel.Page);
- PageChanged(sender, args);
- }
- }
- }
- #endregion
- #region 计算页码
- /// <summary>
- /// 计算页码
- /// </summary>
- private void CalcPageNumList()
- {
- PageCount = (RecordCount - 1) / PageSize + 1; //计算总页数PageCount
- _list = new List<PageControlItemModel>();
- //第一页
- PageControlItemModel item = new PageControlItemModel(1, Page);
- _list.Add(item);
- //当前页码连续页码
- for (int i = Page - ContinuousCount; i <= Page + ContinuousCount; i++)
- {
- if (i > 0 && i < PageCount)
- {
- item = new PageControlItemModel(i, Page);
- if (!_list.Exists(a => a.Page == item.Page))
- {
- _list.Add(item);
- }
- }
- }
- //最后一页
- item = new PageControlItemModel(PageCount, Page);
- if (!_list.Exists(a => a.Page == item.Page))
- {
- _list.Add(item);
- }
- for (int i = _list.Count - 1; i > 0; i--)
- {
- if (_list[i].Page - _list[i - 1].Page > 1)
- {
- _list.Insert(i, new PageControlItemModel(0, Page, 2));
- }
- }
- //上一页下一页
- if (Page == 1)
- {
- this.btnPrePage.IsEnabled = false;
- this.btnPrePage.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#88dddddd"));
- }
- else
- {
- this.btnPrePage.IsEnabled = true;
- this.btnPrePage.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#fff"));
- }
- if (Page == PageCount)
- {
- this.btnNextPage.IsEnabled = false;
- this.btnNextPage.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#88dddddd"));
- }
- else
- {
- this.btnNextPage.IsEnabled = true;
- this.btnNextPage.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#fff"));
- }
- _collection.Clear();
- _list.ForEach(a => { _collection.Add(a); });
- this.itemsControl.ItemsSource = _collection;
- }
- #endregion
- #region 上一页
- private void btnPrePage_Click(object sender, RoutedEventArgs e)
- {
- int prePage = Page - 1;
- if (prePage < 1) prePage = 1;
- if (prePage != Page)
- {
- Page = prePage;
- CalcPageNumList();
- PageChangedEventArgs args = new PageChangedEventArgs(prePage);
- PageChanged(sender, args);
- }
- }
- #endregion
- #region 下一页
- private void btnNextPage_Click(object sender, RoutedEventArgs e)
- {
- int nextPage = Page + 1;
- if (nextPage > PageCount) nextPage = PageCount;
- if (nextPage != Page)
- {
- Page = nextPage;
- CalcPageNumList();
- PageChangedEventArgs args = new PageChangedEventArgs(nextPage);
- PageChanged(sender, args);
- }
- }
- #endregion
- #region INotifyPropertyChanged接口
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string name)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(name));
- }
- }
- #endregion
- }
- #region 分页控件Item Model
- /// <summary>
- /// 分页控件Item Model
- /// </summary>
- public class PageControlItemModel : INotifyPropertyChanged
- {
- private int _Type = 1;
- /// <summary>
- /// 类型(1数字 2省略号)
- /// </summary>
- public int Type
- {
- get { return _Type; }
- set
- {
- _Type = value;
- OnPropertyChanged("Type");
- if (_Type == 1)
- {
- NumVisible = Visibility.Visible;
- OmitVisible = Visibility.Collapsed;
- }
- else
- {
- NumVisible = Visibility.Collapsed;
- OmitVisible = Visibility.Visible;
- }
- }
- }
- private bool _IsCurrentPage;
- /// <summary>
- /// 是否当前页码
- /// </summary>
- public bool IsCurrentPage
- {
- get { return _IsCurrentPage; }
- set
- {
- _IsCurrentPage = value;
- OnPropertyChanged("IsCurrentPage");
- if (_IsCurrentPage)
- {
- CurrentPageColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00f0ff"));
- }
- else
- {
- CurrentPageColor = new SolidColorBrush(Colors.White);
- }
- }
- }
- private SolidColorBrush _CurrentPageColor = new SolidColorBrush(Colors.White);
- /// <summary>
- /// 当前页码颜色
- /// </summary>
- public SolidColorBrush CurrentPageColor
- {
- get { return _CurrentPageColor; }
- set
- {
- _CurrentPageColor = value;
- OnPropertyChanged("CurrentPageColor");
- }
- }
- private int _Page;
- /// <summary>
- /// 页码
- /// </summary>
- public int Page
- {
- get { return _Page; }
- set
- {
- _Page = value;
- OnPropertyChanged("Page");
- }
- }
- private Visibility _NumVisible = Visibility.Visible;
- /// <summary>
- /// 数字可见
- /// </summary>
- public Visibility NumVisible
- {
- get { return _NumVisible; }
- set
- {
- _NumVisible = value;
- OnPropertyChanged("NumVisible");
- }
- }
- private Visibility _OmitVisible = Visibility.Collapsed;
- /// <summary>
- /// 省略号可见
- /// </summary>
- public Visibility OmitVisible
- {
- get { return _OmitVisible; }
- set
- {
- _OmitVisible = value;
- OnPropertyChanged("OmitVisible");
- }
- }
- /// <summary>
- /// 分页控件Item Model
- /// </summary>
- /// <param name="page">页码</param>
- /// <param name="currentPage">当前页码</param>
- /// <param name="type">类型(1数字 2省略号)</param>
- public PageControlItemModel(int page, int currentPage, int type = 1)
- {
- Type = type;
- Page = page;
- IsCurrentPage = page == currentPage;
- }
- #region INotifyPropertyChanged接口
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string name)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(name));
- }
- }
- #endregion
- }
- #endregion
- #region 分页事件参数
- /// <summary>
- /// 分页事件参数
- /// </summary>
- public class PageChangedEventArgs : EventArgs
- {
- private int _Page = 1;
- /// <summary>
- /// 当前页码
- /// </summary>
- public int Page
- {
- get
- {
- return _Page;
- }
- }
- /// <summary>
- /// 分页事件参数
- /// </summary>
- /// <param name="page">当前页码</param>
- public PageChangedEventArgs(int page)
- {
- _Page = page;
- }
- }
- #endregion
- }
|