WPF分页控件 参照百度分页实现 带源码

2017-04-08 20:15:32来源:CSDN作者:xinyuxiong人点击

前段时间项目需要WPF分页控件,百度了许久,也没有找到一个合适的开源控件,不是太丑,就是封装的控件无法使用,最后决定自己写一个。
写的时候,参照百度搜索分页对传统分页控件做了简化。细致地看了下百度搜索分页功能,百度分页控件默认是10页(如果结果列表小于10页,则显示实际页数),除了10个页面元素外,最左和最右两侧各有一个按纽,即前一页和后一页;

  • 传统分页控件中有首页和末页功能,百度弃之;
  • 传统分页控件中有分页容量设置功能,百度弃之;
  • 传统分页控件中有输入页码跳转功能,百度亦弃之;

这么多功能都没有,这个分页能好用嘛,又仔细研究,于是发现一个小秘密,百度分页的10个小页面元素,以第6个为中心,当点击任一个页码,会将该页码从当前位置移动到第6个位置,从起始到结束位置的其他页码都顺延移动,这样一来,在分页导航的过程中,使用起来确实也很方便,最大程序减少了点击次数,也简化了操作,不需要忆和分别那么多功能,想分页,就click。这种简化的背后,其实暗含了细致的用户使用行为分析,即一个人在使用分页的过程中,因为通常不太会记得或者知道自己的想要的要素在哪一页,更倾向于按顺序查看各页,因此列出10个页加上click当然是最简单又能满足需求的方式,通过一个移动选中页到中心位置,实际上一次点击,加载多个可能想看的页面,使10个页面的范围尽量扩大,避免了频繁点击[前一页]、[后一页]的无聊操作,这样一来,即使功能大大简化,但使用起来似乎更方便了。一个小小的功能,都能看出用心可贵。

闲话不多说,看下控件真身:
这里写图片描述

再看下百度的:
这里写图片描述

是不是很像,哈哈,自己做的,美工还欠点;

上代码:

XAML就不上了,后面直接提供个下载的;

.cs

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using Secret.Judgement.Data;
using Secret.Judgement.Log;

namespace Secret.Judgement.UI.UC
{
///
/// ChangePage.xaml 的交互逻辑
///
public partial class DataPager2 : UserControl
{
int _totalPages = 10;
//控件初始化时需设置;
public int TotalPages
{
get { return _totalPages; }
set {
_totalPages = value;

            if (_totalPages < 10)            {                for (int i = _totalPages + 1; i < 11; i++)                {                    Button curBtn = Wrap1.Children[i] as Button;                    curBtn.Visibility = System.Windows.Visibility.Hidden;                    curBtn.Width = 0;                    //curBtn.Margin = ""                }            }        }    }    int _currentPage=1, _prevPage=1;    public int CurrentPage    {        get { return _currentPage; }        set {            _currentPage = value;        }    }    //int _beginPage, _endPage;    Button _cancelbtn, _selectBtn;    //对外暴露的事件,页码改变后触发;    public myDelegate.changePage  pageIndexChanged;    public DataPager2()    {        InitializeComponent();        if (_totalPages < 10)        {            for(int i = _totalPages;i<11;i++)                Wrap1.Children[i].Visibility = System.Windows.Visibility.Hidden;        }    }    private void Button_Click_1(object sender, RoutedEventArgs e)    {        //_selectBtn = e.Source as Button;        ClickHandler((e.Source as Button).Content.ToString());    }    //private LoadPage    //该函数在分页按纽元素点击事件中处理了,按纽的可见性,    //主要是考虑首页、尾页、页数小于10的特殊情况下可见性;    private void ClickHandler(string buttonContent)    {        _prevPage = _currentPage;        if (buttonContent == "◀")        {            if (_currentPage == 2)            {                Wrap1.Children[0].Visibility = System.Windows.Visibility.Hidden;            }            Wrap1.Children[11].Visibility = System.Windows.Visibility.Visible;            _currentPage--;        }        else if (buttonContent == "▶")        {            if (_currentPage == (_totalPages-1))            {                Wrap1.Children[11].Visibility = System.Windows.Visibility.Hidden;            }            Wrap1.Children[0].Visibility = System.Windows.Visibility.Visible;            _currentPage++;        }        else         {            Wrap1.Children[0].Visibility = System.Windows.Visibility.Visible;            Wrap1.Children[11].Visibility = System.Windows.Visibility.Visible;            _currentPage = Convert.ToInt16(buttonContent);            if (_prevPage == _currentPage) return;        }        ChangePage();    }    //关键:想清楚顺移的逻辑,控制好移动步骤;    private void ChangePage()    {        int moveSteps = 0;        int endPage, beginPage;        beginPage = Convert.ToInt16((Wrap1.Children[1] as Button).Content);        if (_totalPages > 10)            endPage = Convert.ToInt16((Wrap1.Children[10] as Button).Content);        else            endPage = _totalPages;        if (_currentPage > _prevPage)        {            moveSteps = _currentPage - _prevPage;            if ((moveSteps + endPage) < _totalPages )            {                for (int i = 1; i < 11; i++)                {                    (Wrap1.Children[i] as Button).Content = Convert.ToInt16((Wrap1.Children[i] as Button).Content) + moveSteps;                }            }            else            {                moveSteps = _totalPages - endPage;                for (int i = 1; i < 11; i++)                {                    (Wrap1.Children[i] as Button).Content = Convert.ToInt16((Wrap1.Children[i] as Button).Content) + moveSteps;                }            }        }        else        {            moveSteps =  _prevPage-_currentPage ;            if (beginPage>moveSteps )            {                for (int i = 1; i < 11; i++)                {                    (Wrap1.Children[i] as Button).Content = Convert.ToInt16((Wrap1.Children[i] as Button).Content) -moveSteps;                }            }            else            {                moveSteps = beginPage-1;                for (int i = 1; i < 11; i++)                {                    (Wrap1.Children[i] as Button).Content = Convert.ToInt16((Wrap1.Children[i] as Button).Content) - moveSteps;                }            }        }        setbuttonColor();        if (pageIndexChanged != null)            pageIndexChanged(_currentPage);    }    private void setbuttonColor()    {        for (int i = 1; i < 11; i++)        {            if (Convert.ToInt16((Wrap1.Children[i] as Button).Content) == _currentPage)                _selectBtn = Wrap1.Children[i] as Button;            if (Convert.ToInt16((Wrap1.Children[i] as Button).Content) == _prevPage)                _cancelbtn = Wrap1.Children[i] as Button;        }        //_selectBtn = Wrap1.Children[_currentPage ] as Button;        //_cancelbtn = Wrap1.Children[_prevPage] as Button;        _selectBtn.Background = Brushes.White;        _cancelbtn.Background = Brushes.LightGray;     }    private void Button_Click_2(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_3(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_4(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_5(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_6(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_7(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_8(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_9(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_10(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_11(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }    private void Button_Click_12(object sender, RoutedEventArgs e)    {        ClickHandler((e.Source as Button).Content.ToString());    }}

}

代码大约150行,并不多,写完调试大约用了3个小时。XAML及类文件下载地址:http://download.csdn.net/detail/xinyuxiong/9807494

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台