步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.0 KiB

using System;
using System.Windows;
using System.Windows.Controls;
namespace BBWY.Controls
{
/// <summary>
/// PageControl.xaml 的交互逻辑
/// </summary>
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;
}
/// <summary>
/// 分页事件
/// </summary>
public event RoutedEventHandler OnPageIndexChanged;
/// <summary>
/// 分页参数
/// </summary>
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();
})));
/// <summary>
/// 当前页数
/// </summary>
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));
/// <summary>
/// 每页记录数
/// </summary>
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));
/// <summary>
/// 总页数
/// </summary>
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();
})));
/// <summary>
/// 总记录数
/// </summary>
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;
//其余自行扩展
}
}