WPF分頁控件 參照百度分頁實現 帶源碼

前段時間項目需要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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章