1. 程式人生 > >WPF使用MVVM實現數據分頁組件

WPF使用MVVM實現數據分頁組件

tac evm sign agen mman rop img show item

技術分享圖片

前臺代碼

<UserControl x:Class="GasWpf.View.ucPage"
             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"
xmlns:local="clr-namespace:GasWpf.View" mc:Ignorable="d" d:DesignHeight="50" d:DesignWidth="800"> <DockPanel Margin="12"> <TextBlock DockPanel.Dock="Left" Margin="0,0,6,0" FontSize="16" VerticalAlignment="Center">Show</TextBlock> <
TextBox DockPanel.Dock="Left" Width="30" MaxLength="3" FontSize="16" Text="{Binding PageSize,Mode=TwoWay}"> </TextBox> <TextBlock DockPanel.Dock="Left" VerticalAlignment="Center" FontSize="16">/ Page</TextBlock> <StackPanel Orientation="Horizontal" DockPanel.Dock
="Right"> <TextBlock VerticalAlignment="Center" FontSize="16" Margin="0,0,12,0"> <TextBlock>Total</TextBlock> <TextBlock Text="{Binding Total}" Foreground="#ed4014"/> </TextBlock> <ItemsControl ItemsSource="{Binding PageNumber}"> <ItemsControl.ItemTemplate> <DataTemplate> <WrapPanel> <Button Command="{Binding PageChange}" CommandParameter="{Binding Value}" ToolTip="{Binding ToolTip}" IsEnabled="{Binding IsEnabled}" Foreground="#515a6e" BorderBrush="{Binding BackGround}" Background="{Binding BackGround}" Content="{Binding Name}" Margin="3,0,3,0" /> </WrapPanel> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </StackPanel> <TextBlock></TextBlock> </DockPanel> </UserControl>

cs代碼

技術分享圖片
using GalaSoft.MvvmLight.Command;
using GasWpf.Model;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace GasWpf.View
{
    /// <summary>
    /// ucPage.xaml 的交互邏輯
    /// </summary>
    public partial class ucPage : UserControl
    {

        public ucPage()
        {
            InitializeComponent();
        }

        #region 每頁顯示的條數
        /// <summary>
        /// 註冊每頁數
        /// </summary>
        public static readonly DependencyProperty PageSizeProperty = 
            DependencyProperty.Register("PageSize", typeof(int), typeof(ucPage),
                new PropertyMetadata(new PropertyChangedCallback(OnPageSizeChanged)));

        /// <summary>
        /// 每頁數
        /// </summary>
        public int PageSize
        {
            get { return Convert.ToInt32(GetValue(PageSizeProperty)); }
            set
            {
                SetValue(PageSizeProperty, value);
            }
        }
        #endregion

        #region 當前頁
        /// <summary>
        /// 註冊當前頁
        /// </summary>
        public static readonly DependencyProperty PageIndexProperty = 
            DependencyProperty.Register("PageIndex", typeof(int), typeof(ucPage),
                new PropertyMetadata(new PropertyChangedCallback(OnPageChanged)));

        /// <summary>
        /// 當前頁
        /// </summary>
        public int PageIndex
        {
            get { return Convert.ToInt32(GetValue(PageIndexProperty)); }
            set
            {
                SetValue(PageIndexProperty, value);
            }
        }
        private static void OnPageSizeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ucPage uc = (ucPage)sender;
            uc.PageIndex = 1;
            uc.pagination();
        }
        private static void OnPageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((ucPage)sender).pagination();
        }
        #endregion

        #region 總記錄數
        /// <summary>
        /// 總記錄數
        /// </summary>
        public static readonly DependencyProperty TotalProperty = 
            DependencyProperty.Register("Total", typeof(int), typeof(ucPage),
                new PropertyMetadata(new PropertyChangedCallback(OnPageChanged)));

        /// <summary>
        /// 總記錄數
        /// </summary>
        public int Total
        {
            get { return Convert.ToInt32(GetValue(TotalProperty)); }
            set
            {
                SetValue(TotalProperty, value);
            }
        }
        #endregion

        private void pagination()
        {
            if (Total == 0 || PageSize ==0) return;

            int _totalPage = Total / PageSize;
            if (Total % PageSize > 0)
            {
                _totalPage++;
            }
            List<Paging> pn = new List<Paging>
            {
                new Paging() {
                    Name = "<",
                    ToolTip = "上一頁",
                    PageChange = Changed(),
                    Value = PageIndex -1,
                    IsEnabled  = PageIndex!=1&&_totalPage!=1
                }
            };
            pn.Add(new Paging()
            {
                Name = "1",
                ToolTip = "1",
                Value = 1,
                PageChange = Changed(),
                IsEnabled = true,
                BackGround = PageIndex == 1 ? "#b39ddb" : "#ede7f6"
            });
            int begin = PageIndex >= 6 ? PageIndex - 3 : 2;
            int end = PageIndex + 3 >= _totalPage ? _totalPage - 1 : PageIndex + 3;
            for (int i = begin; i <= end; i++)
            {
                Paging p = new Paging()
                {
                    Value = i,
                    Name = i.ToString(),
                    IsEnabled = true,
                    ToolTip = i.ToString(),
                    PageChange = Changed()
                };
                if (i == PageIndex)
                {
                    p.BackGround = "#b39ddb";
                }
                if (i == begin && PageIndex - begin >= 3 && PageIndex > 5)
                {
                    p.Value = PageIndex - 5;
                    p.Name = "...";
                    p.ToolTip = "向前5頁";
                }
                else if (i == end && end - PageIndex >= 3 && _totalPage - PageIndex >= 5)
                {
                    p.Value = PageIndex + 5;
                    p.Name = "...";
                    p.ToolTip = "向後5頁";
                }
                pn.Add(p);
            }
            if (_totalPage > 1)
            {
                pn.Add(new Paging()
                {
                    Name = _totalPage.ToString(),
                    ToolTip = _totalPage.ToString(),
                    PageChange = Changed(),
                    Value = _totalPage,
                    IsEnabled = true,
                    BackGround = PageIndex == _totalPage ? "#b39ddb" : "#ede7f6"
                });
            }
            pn.Add(new Paging()
            {
                Name = ">",
                ToolTip = "下一頁",
                Value = PageIndex + 1,
                PageChange = Changed(),
                IsEnabled = PageIndex != _totalPage && _totalPage != 1
            });
            PageNumber = pn;
        }

        private RelayCommand<int> Changed()
        {
            return new RelayCommand<int>((arg) =>
            {
                PageIndex = arg;
            });
        }

        public static readonly DependencyProperty PageNumberProperty =
            DependencyProperty.Register("PageNumber", typeof(List<Paging>), typeof(ucPage));

        /// <summary>
        /// 分頁信息
        /// </summary>
        public List<Paging> PageNumber
        {
            get { return (List<Paging>)GetValue(PageNumberProperty); }
            set
            {
                SetValue(PageNumberProperty, value);
            }
        }
    }
}
View Code

ViewModel代碼

技術分享圖片
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GasWpf.Model;
using System;
using System.Collections.Generic;
using System.Windows.Input;

namespace GasWpf.ViewModel
{
    public class PageVM : ViewModelBase
    {

        public PageVM()
        {
        }

        private void pagination()
        {
            TotalPage = Total / PageSize;
            if (Total % PageSize > 0)
            {
                TotalPage++;
            }
            List<Paging> pn = new List<Paging>
            {
                new Paging() {
                    Name = "<",
                    ToolTip = "上一頁",
                    PageChange = Changed(),
                    Value = _pageIndex -1,
                    IsEnabled  = _pageIndex!=1
                }
            };
            pn.Add(new Paging()
            {
                Name = "1",
                ToolTip = "1",
                Value =1,
                PageChange = Changed(),
                IsEnabled = true,
                BackGround = _pageIndex==1? "#b39ddb" : "#ede7f6"
            });
            int begin = _pageIndex >= 6 ? _pageIndex - 3 : 2;
            int end = _pageIndex + 3 >= TotalPage ? TotalPage-1 : _pageIndex + 3;
            for (int i = begin; i <= end; i++)
            {
                Paging p = new Paging()
                {
                    Value = i,
                    Name = i.ToString(),
                    IsEnabled = true,
                    ToolTip = i.ToString(),
                    PageChange = Changed()
                };
                if (i == _pageIndex)
                {
                    p.BackGround = "#b39ddb";
                }
                if (i == begin && _pageIndex - begin >= 3 && _pageIndex > 5)
                {
                    p.Value = _pageIndex - 5;
                    p.Name = "...";
                    p.ToolTip = "向前5頁";
                }
                else if (i==end && end - _pageIndex >= 3 && TotalPage - _pageIndex >= 5)
                {
                    p.Value = _pageIndex + 5;
                    p.Name = "...";
                    p.ToolTip = "向後5頁";
                }
                pn.Add(p);
            }
            if (TotalPage > 1)
            {
                pn.Add(new Paging()
                {
                    Name = TotalPage.ToString(),
                    ToolTip = TotalPage.ToString(),
                    PageChange = Changed(),
                    Value = TotalPage,
                    IsEnabled = true,
                    BackGround = _pageIndex == TotalPage ? "#b39ddb" : "#ede7f6"
                });
            }
            pn.Add(new Paging()
            {
                Name = ">",
                ToolTip = "下一頁",
                Value = _pageIndex+1,
                PageChange = Changed(),
                IsEnabled = _pageIndex != TotalPage
            });
            PageNumber = pn;
        }

        private RelayCommand<int> Changed()
        {
            return new RelayCommand<int>((arg) =>
            {
                PageIndex = arg;
            });
        }

        private int _total;
        public int Total
        {
            get { return _total; }
            set { _total = value; RaisePropertyChanged(() => Total); }
        }

        private int _pageSize = 15;
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                _pageSize = value;
                RaisePropertyChanged(() => PageSize);
            }
        }

        private int _pageIndex = 1;
        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                _pageIndex = value;
                RaisePropertyChanged(() => PageIndex);
            }
        }

        private int _totalPage = 0;
        public int TotalPage
        {
            get { return _totalPage; }
            set
            {
                _totalPage = value;
                RaisePropertyChanged(() => TotalPage);
            }
        }

        private List<Paging> _pageNumber;
        public List<Paging> PageNumber
        {
            get { return _pageNumber; }
            set
            {
                _pageNumber = value;
                RaisePropertyChanged(() => PageNumber);
            }
        }
    }
}
View Code

演示程序下載

http://www.jooand.com/case/e

WPF使用MVVM實現數據分頁組件