using System; using System.Windows; using System.Windows.Controls; namespace SJ.Controls { /// /// PageControl.xaml 的交互逻辑 /// public partial class PageControl : UserControl { public PageControl() { InitializeComponent(); this.Loaded += PageControl_Loaded; } private void PageControl_Loaded(object sender, RoutedEventArgs e) { this.PageIndex = 1; pageArgs.PageIndex = this.PageIndex; } /// /// 分页事件 /// public event RoutedEventHandler OnPageIndexChanged; /// /// 分页参数 /// private PageArgs pageArgs = new PageArgs(); public static readonly DependencyProperty PageIndexProperty = DependencyProperty.Register("PageIndex", typeof(int), typeof(PageControl), new PropertyMetadata(1, new PropertyChangedCallback((s, e) => { var pageControl = s as PageControl; pageControl?.OnIndexChanged(); }))); /// /// 当前页数 /// public int PageIndex { get { return (int)GetValue(PageIndexProperty); } set { SetValue(PageIndexProperty, value); } } public static readonly DependencyProperty PageSizeProperty = DependencyProperty.Register("PageSize", typeof(int), typeof(PageControl), new PropertyMetadata(1)); /// /// 每页记录数 /// public int PageSize { get { return (int)GetValue(PageSizeProperty); } set { SetValue(PageSizeProperty, value); } } public static readonly DependencyProperty PageCountProperty = DependencyProperty.Register("PageCount", typeof(int), typeof(PageControl), new PropertyMetadata(1)); /// /// 总页数 /// public int PageCount { get { return (int)GetValue(PageCountProperty); } set { SetValue(PageCountProperty, value); } } public static readonly DependencyProperty RecordCountProperty = DependencyProperty.Register("RecordCount", typeof(int), typeof(PageControl), new PropertyMetadata(0, new PropertyChangedCallback((s, e) => { var pageControl = s as PageControl; pageControl?.OnRecordChanged(); }))); /// /// 总记录数 /// public int RecordCount { get { return (int)GetValue(RecordCountProperty); } set { SetValue(RecordCountProperty, value); } } private void OnRecordChanged() { PageCount = (RecordCount - 1) / PageSize + 1; } private void OnIndexChanged() { pageArgs.PageIndex = this.PageIndex; OnPageIndexChanged?.Invoke(this, pageArgs); } private void Btn_Click(object sender, RoutedEventArgs e) { var btn = sender as BButton; var newPageIndex = Convert.ToInt32(btn.Content); if (newPageIndex != PageIndex) PageIndex = newPageIndex; else OnIndexChanged(); } private void btn_first_Click(object sender, RoutedEventArgs e) { if (PageIndex > 1) PageIndex = 1; } private void btn_up_Click(object sender, RoutedEventArgs e) { if (PageIndex > 1) PageIndex--; } private void btn_next_Click(object sender, RoutedEventArgs e) { if (PageIndex < PageCount) PageIndex++; } private void btn_last_Click(object sender, RoutedEventArgs e) { if (PageIndex < PageCount) PageIndex = PageCount; } } public class PageArgs : RoutedEventArgs { public int PageIndex; //其余自行扩展 } }