WPF 分页控件

刚接触WPF,直接给公司做第一个WPF进销存小项目,分页实用四个button实现的,做完后突然感觉自己太傻了。。。为什么不用自定义用户控件。。于是就写了个简单分页控件

布局:

<UserControl x:Class="Com.FMS.View.UserControls.ZsmPager"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             mc:Ignorable="d" 

             d:DesignHeight="50" d:DesignWidth="480" Loaded="UserControl_Loaded">

    <Grid>

        <WrapPanel>

            <Button Name="btnFirstPage" Content="首页" Width="40" Margin="2" Click="btnFirstPage_Click"></Button>

            <Button Name="btnPageDown" Content="下一页" Width="50" Margin="2" Click="btnPageDown_Click"></Button>

            <Button Name="btnPageUp" Content="上一页" Width="50" Margin="2" Click="btnPageUp_Click"></Button>

            <Button Name="btnEndPage" Content="尾页" Width="40" Margin="2" Click="btnEndPage_Click"></Button>

            <TextBlock Name="txtPagerInfo" Text="当前第【{0}】页,共【{1}】页,共【{2}】条记录" Margin="5"></TextBlock>

        </WrapPanel>

    </Grid>

</UserControl>

交互逻辑代码中主要就定义了一个委托事件,在页码改变的时候可以激发该事件

//***************************************************

//

// 文件名(FileName)  : ZsmPager.xaml.cs

//

// 作者(Author)      : zsm

//

// 创建时间(CreateAt):  2013-03-13 18:17:05

//

// 描述(Description) : 个人分页控件交互逻辑

//

//***************************************************

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;



namespace Com.FMS.View.UserControls

{

    /// <summary>

    /// 定义页码改变后激发的委托事件

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    public delegate void PagerIndexChangedEventHandler(object sender, EventArgs e);



    /// <summary>

    /// ZsmPager.xaml 的交互逻辑

    /// </summary>

    public partial class ZsmPager : UserControl

    {

        #region 私有变量

        /// <summary>

        /// 当前页码

        /// </summary>

        private int _pageIndex = 1;

        /// <summary>

        /// 分页大小

        /// </summary>

        private int _pageSize = 15;

        /// <summary>

        /// 记录总数

        /// </summary>

        private int _totalCount = 0;

        /// <summary>

        /// 总页数

        /// </summary>

        private int _pageCount = 0;

        #endregion



        /// <summary>

        /// 构造

        /// </summary>

        public ZsmPager()

        {

            InitializeComponent();

        }



        #region 事件声明

        /// <summary>

        /// 页码改变后的事件

        /// </summary>

        public event PagerIndexChangedEventHandler PagerIndexChanged;

        #endregion



        #region 属性

        /// <summary>

        /// 当前页码

        /// </summary>

        public int PageIndex 

        {

            get { return _pageIndex; }

            set { _pageIndex = value; }

        }



        /// <summary>

        /// 分页大小

        /// </summary>

        public int PageSize

        {

            get { return _pageSize; }

            set { _pageSize = value; }

        }



        /// <summary>

        /// 记录总数

        /// </summary>

        public int TotalCount 

        {

            get { return _totalCount; }

            set 

            {

                _totalCount = value;

                //计算总页数

                _pageCount = (int)Math.Ceiling((double)_totalCount / _pageSize);

                SetPagerInfo(_pageIndex, _pageCount, _totalCount);

            }

        }



        /// <summary>

        /// 总页数

        /// </summary>

        public int PageCount 

        {

            get { return _pageCount; }

            set { _pageCount = value; }

        }



        #endregion



        #region 私有方法

        /// <summary>

        /// 设置分页按钮是否可用

        /// </summary>

        private void SetPageButtonEnabled()

        {

            //确定分页按钮的是否可用

            if (_pageCount <= 1)

            {

                btnPageDown.IsEnabled = false;

                btnPageUp.IsEnabled = false;

                btnEndPage.IsEnabled = false;

            }

            else

            {

                if (_pageIndex == _pageCount)

                {

                    btnPageDown.IsEnabled = false;

                    btnPageUp.IsEnabled = true;

                    btnEndPage.IsEnabled = false;

                }

                else if (_pageIndex <= 1)

                {

                    btnPageDown.IsEnabled = true;

                    btnPageUp.IsEnabled = false;

                    btnEndPage.IsEnabled = true;

                }

                else

                {

                    btnPageDown.IsEnabled = true;

                    btnPageUp.IsEnabled = true;

                    btnEndPage.IsEnabled = true;

                }

            }

        }



        /// <summary>

        /// 设置控件显示信息

        /// </summary>

        /// <param name="pageIndex">当前页码</param>

        /// <param name="pageCount">共有页数</param>

        /// <param name="totalCount">总记录条数</param>

        private void SetPagerInfo(int pageIndex, int pageCount, int totalCount)

        {

            txtPagerInfo.Text = String.Format("当前第【{0}】页,共【{1}】页,共【{2}】条记录", pageIndex, pageCount, totalCount);

        }

        #endregion



        #region 私有事件

        /// <summary>

        /// 控件加载事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void UserControl_Loaded(object sender, RoutedEventArgs e)

        {

            SetPageButtonEnabled();

        }   



        /// <summary>

        /// 首页按钮事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnFirstPage_Click(object sender, RoutedEventArgs e)

        {

            _pageIndex = 1;

            SetPageButtonEnabled();

            PagerIndexChanged(sender, e);

        }



        /// <summary>

        /// 下一页按钮事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnPageDown_Click(object sender, RoutedEventArgs e)

        {

            _pageIndex++;

            SetPageButtonEnabled();

            PagerIndexChanged(sender, e);

        }



        /// <summary>

        /// 上一页按钮事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnPageUp_Click(object sender, RoutedEventArgs e)

        {

            _pageIndex--;

            SetPageButtonEnabled();

            PagerIndexChanged(sender, e);

        }



        /// <summary>

        /// 尾页按钮事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnEndPage_Click(object sender, RoutedEventArgs e)

        {

            _pageIndex = _pageCount;

            SetPageButtonEnabled();

            PagerIndexChanged(sender, e);

        }

        #endregion

    }

}

使用分页控件时的xaml

<my:ZsmPager x:Name="pagerUser" PagerIndexChanged="pagerUser_PagerIndexChanged" />     

<Window>中添加xmlns:my="clr-namespace:命名空间"

在时实现控件PagerIndexChanged事件即可

/// <summary>

        /// 页码改变时激发该事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void pagerUser_PagerIndexChanged(object sender, EventArgs e)

        {

            //DataGrid数据绑定方法

            DgUserDataBind();

        }

样式简单,不漂亮,一般分页可以用,界面:

你可能感兴趣的:(WPF)