WPF 分页控件

控件 页面

<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Style="{StaticResource stcPager}">
        <Button Name="FirstButton" Content="首页" Click="Button_Click" Style="{StaticResource btnPager}"/>
        <Button Name="PreButton" Content="上一页" Click="Button_Click" Style="{StaticResource btnPager}"/>

        <ItemsControl Name="icPage">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding text}" Click="Button_Click" Style="{Binding style}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

        <Button Name="NextButton" Content="下一页" Click="Button_Click" Style="{StaticResource btnPager}"/>
        <Button Name="LastButton" Content="尾页" Click="Button_Click" Style="{StaticResource btnPager}"/>

        <TextBlock Text="转到 " Style="{StaticResource txtPager}"/>
        <ComboBox Name="cbGoto" IsEditable="True"/>

        <TextBlock Text="  共" Style="{StaticResource txtPager}"/>
        <TextBlock Name="tbPageCount" Style="{StaticResource txtPager}"/>
        <TextBlock Text="页" Style="{StaticResource txtPager}"/>
        <TextBlock Text=" / " Style="{StaticResource txtPager}"/>
        <TextBlock Text="共" Style="{StaticResource txtPager}"/>
        <TextBlock Name="tbRecordCount" Style="{StaticResource txtPager}"/>
        <TextBlock Text="条 " Style="{StaticResource txtPager}"/>

        <DockPanel Visibility="Collapsed">
            <TextBlock Text="当前第" Style="{StaticResource txtPager}"/>
            <TextBlock Name="tbPageIndex" Style="{StaticResource txtPager}"/>
            <TextBlock Text="页" Style="{StaticResource txtPager}"/>
        </DockPanel>
    </StackPanel>

控件页 后台

 public delegate void PageIndexChangingEventHandler(int pageIndex, EventArgs e);

    /// <summary>
    /// Interaction logic for BtnPager.xaml
    /// </summary>
    public partial class BtnPage: UserControl
    {
        public BtnPage()
        {
            InitializeComponent();
        }

        private int pageIndex = 1;
        public int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }

        private int pageSize = 10;
        /// <summary>
        /// 页面记录数 应与DataGrid的PageSize保持一致
        /// </summary>
        public int PageSize
        {
            get { return pageSize; }
            set
            {
                //if (value > 0)
                //{
                    pageSize = value;
                    Load();
                //}
            }
        }

        private int recordcount = 0;
        /// <summary>
        /// 记录总数
        /// </summary>
        public int RecordCount
        {
            get { return recordcount; }
            set
            {
                //if (value > 0)
                //{
                    recordcount = value;
                    Load();
                //}
            }
        }

        private int pageCount;

        private int maxPageBtnNum = 8;
        /// <summary>
        /// 要显示的页数
        /// </summary>
        public int MaxPageBtnNum
        {
            get { return maxPageBtnNum; }
            set { if (value != 0) maxPageBtnNum = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public event PageIndexChangingEventHandler PageIndexChangingEvent;

        / <summary>
        / button Content对应的值
        / </summary>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;
            switch (bt.Name)
            {
                case "FirstButton":
                    pageIndex = 1;
                    break;
                case "LastButton":
                    pageIndex = pageCount;
                    break;
                case "PreButton":
                    if (pageIndex > 1) pageIndex--;
                    break;
                case "NextButton":
                    if (pageIndex < pageCount) pageIndex++;
                    break;
                default:
                    pageIndex = int.Parse(bt.Content.ToString());
                    break;
            }
            PageChangeSet(e);
        }

        /// <summary>
        /// 设置button的Content
        /// </summary>
        private void BindList()
        {
            if (pageIndex > 1)
            {
                FirstButton.IsEnabled = true;
                PreButton.IsEnabled = true;
            }
            else
            {
                FirstButton.IsEnabled = false;
                PreButton.IsEnabled = false;
            }

            if (pageIndex < pageCount)
            {
                LastButton.IsEnabled = true;
                NextButton.IsEnabled = true;
            }
            else
            {
                LastButton.IsEnabled = false;
                NextButton.IsEnabled = false;
            }

            int halfNum = maxPageBtnNum % 2 == 0 ? maxPageBtnNum / 2 : (maxPageBtnNum / 2 + 1);

            int fromIndex, toIndex;

            if (pageIndex <= halfNum)
                fromIndex = 1;
            else
                fromIndex = pageIndex - halfNum;

            if (pageCount - pageIndex <= halfNum)
                toIndex = pageCount;
            else
                toIndex = pageIndex + halfNum;

            ArrayList btnIndexList = new ArrayList();
            for (int i = fromIndex; i <= toIndex; i++)
            {
                if (i == pageIndex)
                    btnIndexList.Add(new { text = i, style = this.FindResource("btnPager_Act") as Style });
                else
                    btnIndexList.Add(new { text = i, style = this.FindResource("btnPager") as Style });
            }
            icPage.ItemsSource = btnIndexList;
            icPage.Items.Refresh();

            List<int> cbbIndexList = new List<int>();
            for (int i = 1; i <= pageCount; i++)
            {
                cbbIndexList.Add(i);
            }

            cbGoto.SelectionChanged -= new SelectionChangedEventHandler(cbGoto_SelectionChanged);
            cbGoto.ItemsSource = cbbIndexList;
            cbGoto.Items.Refresh();
            cbGoto.Text = pageIndex.ToString();
            cbGoto.SelectionChanged += new SelectionChangedEventHandler(cbGoto_SelectionChanged);

            tbPageCount.Text = pageCount.ToString();
            tbRecordCount.Text = RecordCount.ToString();
            tbPageIndex.Text = pageIndex.ToString();
        }

        /// <summary>
        /// 翻页前的设置
        /// </summary>
        /// <param name="e"></param>
        private void PageChangeSet(EventArgs e)
        {
            BindList();
            PageIndexChangingEvent(PageIndex, e);
        }

        /// <summary>
        /// 加载,当数据条数变化时,重新加载
        /// </summary>
        private void Load()
        {
            pageCount = (recordcount % pageSize == 0) ? recordcount / pageSize : (recordcount / pageSize + 1);

            BindList();
        }

        /// <summary>
        /// 页面下拉框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbGoto_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                pageIndex = int.Parse(cbGoto.SelectedValue.ToString());

                PageChangeSet(e);
            }
            catch (Exception) { }
        }
    }
使用页 后台

service.GetPageList(PageIndex, PageSize, OrderBy, strsql, ref count);
dgplist.PageIndex = pageIndex;
dgplist.PageSize = pageSize;
dgplist.RecordCount = count;


分页方法

/// <summary>
        /// 分页
        /// </summary>
        /// <param name="PageIndex">页索引</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="Table">表名</param>
        /// <param name="Field">返回的字段为*表示所有的字段</param>
        /// <param name="OrderBy">排序规则</param>
        /// <param name="PK">主键列,如果表没有主键,随便写一个列即可</param>
        /// <param name="Filter">过滤条件</param>
        /// <param name="MaxPage"> 执行结果 -1 error, 0 false, 大于1表示一共有几页</param>
        /// <param name="TotalRow">记录总数</param>
        /// <param name="Descript">对分页的描述</param>
        /// <returns></returns>
        public static DataSet GetPageList(int PageIndex, int PageSize, string Table, string Field, string OrderBy, string PK, string Filter, ref int MaxPage, ref int TotalRow, ref string Descript)//out是在外面不能赋值的
        {
            if (Filter == null)
            {
                Filter = "";
            }
            SqlParameter[] pars = new SqlParameter[] {
                new SqlParameter("@PageIndex", PageIndex),
                new SqlParameter("@PageSize", PageSize),
                new SqlParameter("@Table", Table),
                new SqlParameter("@Field", Field),
                new SqlParameter("@OrderBy", OrderBy),
                new SqlParameter("@PK", PK),
                new SqlParameter("@Filter", Filter),
                new SqlParameter("@MaxPage", MaxPage),
                 new SqlParameter("@TotalRow", TotalRow),
                  new SqlParameter("@Descript", Descript)
            };
            pars[7].Direction = ParameterDirection.Output;
            pars[8].Direction = ParameterDirection.Output;
            pars[9].Direction = ParameterDirection.Output;
            DataSet ds = RunProcedure("RowNumberPagination", pars, "ds");
            MaxPage = (int)pars[7].Value;
            TotalRow = (int)pars[8].Value;
            Descript = pars[9].Value.ToString();
            return ds;
        }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值