翻页PagerSimple

XAML

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock Height="20" Text="当前" VerticalAlignment="Center" />
    <TextBlock Height="20" Text="{Binding CurrentPageCount, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=TwoWay}" Foreground="Red" VerticalAlignment="Center" />
    <TextBlock Height="20" Text="条记录  " VerticalAlignment="Center" />
    <TextBlock Height="20" Text="第" VerticalAlignment="Center" />
    <TextBlock Height="20" Text="{Binding PageIndex, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=TwoWay}" Foreground="Red" VerticalAlignment="Center" />
    <TextBlock Height="20" Text="页 " VerticalAlignment="Center" />

    <Button Name="prePageBtn" Width="50" Margin="2,4" Content="上一页" Click="PrePageBtn_Click"/>
    <Button Name="nextPageBtn" Width="50" Margin="2,4" Content="下一页" Click="NextPageBtn_Click"/>

    <TextBlock Height="20" Text="转到" Margin="5" VerticalAlignment="Center"/>
    <TextBox Name="gotoPageNoTb" Width="40" Margin="2,5" />
    <TextBlock Height="20" Text="页" Margin="2,5" VerticalAlignment="Center"/>
    <Button Content="跳转" Click="GotoBtn_Click" Width="50" Margin="2,4" />
</StackPanel>

CS

/// <summary>
/// PagerSimple.xaml 的交互逻辑
/// </summary>
public partial class PagerSimple : UserControl
{
    /// <summary>
    /// 翻页响应事件
    /// </summary>
    public event EventHandler PageChangedEvent;

    /// <summary>
    /// 每页项目条数,默认25
    /// </summary>
    public int PageSize
    {
        get { return (int)GetValue(PageSizeProperty); }
        set { SetValue(PageSizeProperty, value); }
    }
    public static readonly DependencyProperty PageSizeProperty =
        DependencyProperty.Register("PageSize", typeof(int), typeof(PagerSimple), new PropertyMetadata(25));


    /// <summary>
    /// 当前页索引,1开头
    /// </summary>
    public int PageIndex
    {
        get { return (int)GetValue(PageIndexProperty); }
        set { SetValue(PageIndexProperty, value); }
    }
    public static readonly DependencyProperty PageIndexProperty =
        DependencyProperty.Register("PageIndex", typeof(int), typeof(PagerSimple), new PropertyMetadata(1));


    /// <summary>
    /// 总页数
    /// </summary>
    public int TotalPage
    {
        get { return (int)GetValue(TotalPageProperty); }
        set { SetValue(TotalPageProperty, value); }
    }
    public static readonly DependencyProperty TotalPageProperty =
        DependencyProperty.Register("TotalPage", typeof(int), typeof(PagerSimple), new PropertyMetadata(0));

    /// <summary>
    /// 当前页项目条数
    /// </summary>
    public int CurrentPageCount
    {
        get { return (int)GetValue(CurrentPageCountProperty); }
        set { SetValue(CurrentPageCountProperty, value); }
    }
    public static readonly DependencyProperty CurrentPageCountProperty =
        DependencyProperty.Register("CurrentPageCount", typeof(int), typeof(PagerSimple), new PropertyMetadata(0, OnCurrentPageCountChanged));
    private static void OnCurrentPageCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var ctl = (PagerSimple)d;
        ctl.OnCurrentPageCountChanged((int)e.NewValue);
    }
    private void OnCurrentPageCountChanged(int newValue)
    {
        IsPagerInitialized = false;
        // 更新总页数
        if (newValue == 0 || newValue < PageSize)
        {
            TotalPage = PageIndex;
            this.nextPageBtn.IsEnabled = false;
        }
        else
        {
            this.nextPageBtn.IsEnabled = true;
        }
    }
    /// <summary>
    /// 是否初始化控件设置,true初始化
    /// </summary>
    public bool IsPagerInitialized
    {
        get { return (bool)GetValue(IsPagerInitializedProperty); }
        set { SetValue(IsPagerInitializedProperty, value); }
    }
    public static readonly DependencyProperty IsPagerInitializedProperty =
        DependencyProperty.Register("IsPagerInitialized", typeof(bool), typeof(PagerSimple), new PropertyMetadata(false, OnIsPagerInitializedChanged));
    private static void OnIsPagerInitializedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var ctl = (PagerSimple)d;
        ctl.OnIsPagerInitializedChanged((bool)e.NewValue);
    }
    private void OnIsPagerInitializedChanged(bool newValue)
    {
        if (newValue)
        {
            InitPager();
        }
    }

    public PagerSimple()
    {
        InitializeComponent();
        InitPager();
        // 默认空,不能上下页
        this.prePageBtn.IsEnabled = false;
        this.nextPageBtn.IsEnabled = false;
    }

    void InitPager()
    {
        TotalPage = 10_000_000;
        PageIndex = 1;
        // 首页时没有上一页
        this.prePageBtn.IsEnabled = false;  
        gotoPageNoTb.Text = string.Empty;
    }

    /// <summary>
    /// 事件响应方法
    /// </summary>
    /// <param name="pageNo"></param>
    public void PageChangedEventFunc()
    {
        try
        {
            // 按钮状态  
            this.prePageBtn.IsEnabled = PageIndex > 1;
            this.nextPageBtn.IsEnabled = PageIndex < TotalPage;
            var args = new PagerSimpleEventArgs
            {
                PageIndex = PageIndex,
                PageSize = PageSize,
            };
            PageChangedEvent(this, args);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    // 上一页事件     
    private void PrePageBtn_Click(object sender, RoutedEventArgs e)
    {
        if (PageIndex > 1)
        {
            IsPagerInitialized = false;
            PageIndex--;
            PageChangedEventFunc();
        }
    }

    // 下一页事件    
    private void NextPageBtn_Click(object sender, RoutedEventArgs e)
    {
        if (PageIndex < TotalPage)
        {
            IsPagerInitialized = false;
            PageIndex++;
            PageChangedEventFunc();
        }
    }

    // 跳转事件    
    private void GotoBtn_Click(object sender, RoutedEventArgs e)
    {
        if (this.prePageBtn.IsEnabled == false && this.nextPageBtn.IsEnabled == false)
        {
            return;
        }
        if (int.TryParse(this.gotoPageNoTb.Text, out int pageNo) && pageNo >= 1 && pageNo <= TotalPage)
        {
            IsPagerInitialized = false;
            PageIndex = pageNo;
            PageChangedEventFunc();
        }
    }

}

public class PagerSimpleEventArgs : EventArgs
{
    public int PageIndex { get; set; }
    public int PageSize { get; set; }
}

demo

DtoEntity SelectedItem { get; set; }

private void UserControl_Loaded()
{
    // 默认加载数据;
    Search_Click();
}

private void Search_Click()
{
    SelectedItem.IsPagerInitialized = true;
    LocalParking.TryApiMethod(() => DataBindAsync());
}

private void GetData_Click(object parameter)
{
    if (parameter is PagerSimpleEventArgs e)
    {
        LocalParking.TryApiMethod(() => DataBindAsync(e.PageIndex, e.PageSize));
    }
}

private async Task DataBindAsync(int pageIndex = 1, int pageSize = 25)
{
    // 接口调用
    if (response != null)
    {
        SelectedItem.CurrentPageCount = response.ResultList.Count;
    }
}

public class DtoEntity : BindableBase // NotifyPropertyChangedEx
{
    #region pager
    private int _pagerSize;
    public int PagerSize
    {
        get { return _pagerSize; }
        set { SetProperty(ref _pagerSize, value); }
    }
    private int _currentPageCount;
    public int CurrentPageCount
    {
        get { return _currentPageCount; }
        set { SetProperty(ref _currentPageCount, value); }
    }

    private bool _isPagerInitialized;
    public bool IsPagerInitialized
    {
        get { return _isPagerInitialized; }
        set { SetProperty(ref _isPagerInitialized, value); }
    }
    #endregion
}
原文地址:https://www.cnblogs.com/wesson2019-blog/p/15111374.html