zoukankan      html  css  js  c++  java
  • 简单翻页控件

    wpf

    demo

    // 1
    <UC_PagerSimple x:Name="paper" GetDataDelegateHandler="GetData"/>
    // 2
    private void GetData(int pageNo, int pageSize)
    {
    dgData.ItemsSource = response.Body.ResultList;
    paper.ReceivePageCount(response.Body.ResultList.Count);
    }
    // 3
    paper.GotoFirstPage();

    XAML

    <UserControl>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Height="20" Text="当前" VerticalAlignment="Center" />
            <TextBlock Height="20" Name="currentCountTbk" Foreground="Red" VerticalAlignment="Center" />
            <TextBlock Height="20" Text="条记录  " VerticalAlignment="Center" />
            <TextBlock Height="20" Text="第" VerticalAlignment="Center" />
            <TextBlock Height="20" Name="pageNoTbk" Foreground="Red" VerticalAlignment="Center" />
            <TextBlock Height="20" Text="页 " VerticalAlignment="Center" />
    
            <TextBlock Height="20" Text="每页显示" Visibility="Collapsed" Width="0"/>
            <TextBox Name="pageSizeTb" Visibility="Hidden" Width="0"></TextBox>
            <Button Name="setPageSizeBtn" Content="设置" Visibility="Collapsed" Click="SetPageSizeBtn_Click" Width="0"/>
    
            <Button Name="firstPageBtn" Width="50"  Margin="2,4" Content="首页" Click="FirstPageBtn_Click" Visibility="Collapsed"/>
            <Button Name="prePageBtn" Width="50" Margin="2,4" Content="上一页" Click="PrePageBtn_Click"/>
            <Button Name="nextPageBtn" Width="50" Margin="2,4" Content="下一页" Click="NextPageBtn_Click"/>
            <Button Name="lastPageBtn" Width="50" Margin="2,4" Content="末页" Click="LastPageBtn_Click" Visibility="Collapsed"/>
    
            <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" />
            <Button Content="刷新" Click="RefreshBtn_Click" Visibility="Collapsed"/>
        </StackPanel>
    </UserControl>
    

    cs

    public partial class UC_PagerSimple : UserControl
    {
        /// <summary>
        /// 每页记录数
        /// </summary>
        private int _pageSize = 25;
        /// <summary>
        /// 当前页记录数
        /// </summary>
        private int _currentCount = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        private int _pageCount = 1;
        /// <summary>  
        /// 获取数据委托,返回总记录数  
        /// </summary>  
        /// <param name="pageNo">请求页</param>  
        /// <param name="pageSize">每页记录数</param>  
        /// <returns>总记录数</returns> 
        public Action<int, int> GetDataDelegateHandler
        {
            set; private get;
        }
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                if (value > 0)
                {
                    _pageSize = value;
                }
            }
        }
        /// <summary>
        /// 当前页索引,1开头
        /// </summary>
        public int PageIndex { get; set; } = 1;
    
        public UC_PagerSimple()
        {
            InitializeComponent();
        }
    
        void InitPager()
        {
            this.prePageBtn.IsEnabled = true;
            this.nextPageBtn.IsEnabled = true;
            _pageCount = 10000000;
            _currentCount = 0;
            gotoPageNoTb.Text = string.Empty;
        }
        /// <summary>  
        /// 刷新当前页  
        /// </summary>  
        public void Refresh()
        {
            SendGotoPage(PageIndex);
        }
        /// <summary>
        /// 显示首页数据
        /// </summary>
        public void GotoFirstPage()
        {
            InitPager();
            SendGotoPage(1);
        }
    
        public void GotoLastPage()
        {
            SendGotoPage(_pageCount);
        }
        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="pageNo"></param>
        public void SendGotoPage(int pageNo)
        {
            if (pageNo <= 0)
            {
                pageNo = 1;
            }
    
            this.PageIndex = pageNo;
    
            try
            {
                GetDataDelegateHandler(pageNo, _pageSize);
            }
            catch (Exception)
            {
                this.pageNoTbk.Text = "";
            }
        }
        /// <summary>
        /// 初始化控件显示
        /// </summary>
        /// <param name="itemCount">当前页数</param>
        public void ReceivePageCount(int itemCount)
        {
            try
            {
                if (itemCount < 0)
                {
                    firstPageBtn.IsEnabled = true;
                    lastPageBtn.IsEnabled = false;
    
                    currentCountTbk.Text = "很多";
    
                    InitPager();
                }
                else
                {
                    _currentCount = itemCount;
    
                    // 页码显示  
                    this.currentCountTbk.Text = _currentCount + "";
                    this.pageNoTbk.Text = PageIndex + "";
                    this.pageSizeTb.Text = _pageSize + "";
    
                    // 更新总页数
                    if (itemCount == 0 || itemCount < _pageSize)
                    {
                        _pageCount = PageIndex;
                    }
    
                    // 按钮状态  
                    this.prePageBtn.IsEnabled = PageIndex > 1;
                    this.nextPageBtn.IsEnabled = PageIndex < _pageCount;
                }
            }
            catch (Exception)
            {
                this.pageNoTbk.Text = "";
            }
        }
    
        // 设置页显示记录数   
        private void SetPageSizeBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int pageSize = Convert.ToInt32(this.pageSizeTb.Text);
                if (pageSize > 0)
                {
                    this._pageSize = pageSize;
                    this.GotoFirstPage();
                }
                else
                {
                    this.pageSizeTb.Text = this._pageSize + "";
                }
            }
            catch (Exception)
            {
                this.pageSizeTb.Text = this._pageSize + "";
            }
        }
    
        // 首页事件     
        private void FirstPageBtn_Click(object sender, RoutedEventArgs e)
        {
            GotoFirstPage();
        }
    
        // 上一页事件     
        private void PrePageBtn_Click(object sender, RoutedEventArgs e)
        {
            if (PageIndex > 1)
            {
                PageIndex--;
                SendGotoPage(PageIndex);
            }
        }
    
        // 下一页事件    
        private void NextPageBtn_Click(object sender, RoutedEventArgs e)
        {
            if (PageIndex < _pageCount)
            {
                PageIndex++;
                SendGotoPage(PageIndex);
            }
        }
    
        // 末页事件    
        private void LastPageBtn_Click(object sender, RoutedEventArgs e)
        {
            GotoLastPage();
        }
    
        // 跳转事件    
        private void GotoBtn_Click(object sender, RoutedEventArgs e)
        {
            if (int.TryParse(this.gotoPageNoTb.Text, out int pageNo) && pageNo >= 1 && pageNo <= _pageCount)
            {
                SendGotoPage(pageNo);
            }
        }
    
        // 刷新  
        private void RefreshBtn_Click(object sender, RoutedEventArgs e)
        {
            Refresh();
        }
    
    }
    

    prism

    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

    using System;
    using System.Windows;
    using System.Windows.Controls;
    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)
      {
          // 更新总页数
          if (newValue == 0 || newValue < PageSize)
          {
              TotalPage = PageIndex;
              this.nextPageBtn.IsEnabled = false;
          }
      }
    
      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();
      }
    
      void InitPager()
      {
          this.prePageBtn.IsEnabled = false;
          this.nextPageBtn.IsEnabled = true;
          TotalPage = 10_000_000;
          PageIndex = 1;
          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 (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

    <comm:PagerSimple PageSize="{Binding SelectedItem.PagerSize, Mode=TwoWay}"
                    CurrentPageCount="{Binding SelectedItem.CurrentPageCount, Mode=TwoWay}"
                    IsPagerInitialized="{Binding SelectedItem.IsPagerInitialized, Mode=TwoWay}">
      <b:Interaction.Triggers>
          <b:EventTrigger EventName="PageChangedEvent">
              <b:InvokeCommandAction Command="{Binding GetDataCommand}" PassEventArgsToCommand="True"/>
          </b:EventTrigger>
      </b:Interaction.Triggers>
    </comm:PagerSimple>
    
    private void GetData_Click(object parameter)
    {
       if (parameter is PagerSimpleEventArgs e)
       {
           var indx = e.PageIndex;
           var size = e.PageSize;
       }
    }
    
  • 相关阅读:
    征战蓝桥 —— 2015年第六届 —— C/C++A组第5题——九数组分数
    征战蓝桥 —— 2015年第六届 —— C/C++A组第5题——九数组分数
    征战蓝桥 —— 2015年第六届 —— C/C++A组第4题——格子中输出
    SSLZYC 邮票
    SSLZYC 道路阻塞
    SSLZYC 反射
    SSLZYC 家族
    秒杀系统架构分析与实战
    工厂模式
    工厂模式
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/14676844.html
Copyright © 2011-2022 走看看