博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF 实现 DataGrid/ListView 分页控件
阅读量:5061 次
发布时间:2019-06-12

本文共 10475 字,大约阅读时间需要 34 分钟。

原文:

在WPF中,通常会选用DataGrid/ListView进行数据展示,如果数据量不多,可以直接一个页面显示出来。如果数据量很大,2000条数据,一次性显示在一个页面中,不仅消耗资源,而且用户体验也很糟糕。这篇博客将介绍如何创建一个分页控件。

为了简单起见,这个分页控件目前只有 首页/上一页/下一页/末页/总页数/第几页 等功能。实现思路,首页/上一页/下一页/末页 这四个通过路由事件来实现,在使用时可以使用命令进行绑定,或者直接使用均可。总页数和第几页通过依赖属性来实现,使用时将页数进行绑定显示即可。示例代码如下:

Pager控件:

C#:

public partial class Pager : UserControl    {        public static RoutedEvent FirstPageEvent;        public static RoutedEvent PreviousPageEvent;        public static RoutedEvent NextPageEvent;        public static RoutedEvent LastPageEvent;        public static readonly DependencyProperty CurrentPageProperty;        public static readonly DependencyProperty TotalPageProperty;        public string CurrentPage        {            get { return (string)GetValue(CurrentPageProperty); }            set { SetValue(CurrentPageProperty, value); }        }        public string TotalPage        {            get { return (string)GetValue(TotalPageProperty); }            set { SetValue(TotalPageProperty, value); }        }        public Pager()        {            InitializeComponent();        }        static Pager()        {            FirstPageEvent = EventManager.RegisterRoutedEvent("FirstPage", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(Pager));            PreviousPageEvent = EventManager.RegisterRoutedEvent("PreviousPage", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(Pager));            NextPageEvent = EventManager.RegisterRoutedEvent("NextPage", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(Pager));            LastPageEvent = EventManager.RegisterRoutedEvent("LastPage", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(Pager));            CurrentPageProperty = DependencyProperty.Register("CurrentPage", typeof(string), typeof(Pager), new PropertyMetadata(string.Empty,new PropertyChangedCallback(OnCurrentPageChanged)));            TotalPageProperty = DependencyProperty.Register("TotalPage", typeof(string), typeof(Pager), new PropertyMetadata(string.Empty,new PropertyChangedCallback(OnTotalPageChanged)));        }        public event RoutedEventHandler FirstPage        {            add { AddHandler(FirstPageEvent, value); }            remove { RemoveHandler(FirstPageEvent, value); }        }        public event RoutedEventHandler PreviousPage        {            add { AddHandler(PreviousPageEvent, value); }            remove { RemoveHandler(PreviousPageEvent, value); }        }        public event RoutedEventHandler NextPage        {            add { AddHandler(NextPageEvent, value); }            remove { RemoveHandler(NextPageEvent, value); }        }        public event RoutedEventHandler LastPage        {            add { AddHandler(LastPageEvent, value); }            remove { RemoveHandler(LastPageEvent, value); }        }        public static void OnTotalPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            Pager p = d as Pager;            if(p != null)            {                Run rTotal = (Run)p.FindName("rTotal");                rTotal.Text = (string)e.NewValue;            }        }        private static void OnCurrentPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            Pager p = d as Pager;            if(p != null)            {                Run rCurrrent = (Run)p.FindName("rCurrent");                rCurrrent.Text = (string)e.NewValue;            }        }        private void FirstPageButton_Click(object sender, RoutedEventArgs e)        {            RaiseEvent(new RoutedEventArgs(FirstPageEvent, this));        }        private void PreviousPageButton_Click(object sender, RoutedEventArgs e)        {            RaiseEvent(new RoutedEventArgs(PreviousPageEvent, this));        }        private void NextPageButton_Click(object sender, RoutedEventArgs e)        {            RaiseEvent(new RoutedEventArgs(NextPageEvent, this));        }        private void LastPageButton_Click(object sender, RoutedEventArgs e)        {            RaiseEvent(new RoutedEventArgs(LastPageEvent, this));        }    }
View Code

在MainWindow中,

XAML:

MainViewModel类:

public class MainViewModel : ViewModel    {        private ICommand _firstPageCommand;        public ICommand FirstPageCommand        {            get            {                return _firstPageCommand;            }            set            {                _firstPageCommand = value;            }        }        private ICommand _previousPageCommand;        public ICommand PreviousPageCommand        {            get            {                return _previousPageCommand;            }            set            {                _previousPageCommand = value;            }        }        private ICommand _nextPageCommand;        public ICommand NextPageCommand        {            get            {                return _nextPageCommand;            }            set            {                _nextPageCommand = value;            }        }        private ICommand _lastPageCommand;        public ICommand LastPageCommand        {            get            {                return _lastPageCommand;            }            set            {                _lastPageCommand = value;            }        }        private int _pageSize;        public int PageSize        {            get            {                return _pageSize;            }            set            {                if(_pageSize != value)                {                    _pageSize = value;                    OnPropertyChanged("PageSize");                }            }        }        private int _currentPage;        public int CurrentPage        {            get            {                return _currentPage;            }            set            {                if(_currentPage != value)                {                    _currentPage = value;                    OnPropertyChanged("CurrentPage");                }            }        }        private int _totalPage;        public int TotalPage        {            get            {                return _totalPage;            }            set            {                if(_totalPage != value)                {                    _totalPage = value;                    OnPropertyChanged("TotalPage");                }            }        }        private ObservableCollection
_fakeSoruce; public ObservableCollection
FakeSource { get { return _fakeSoruce; } set { if(_fakeSoruce != value) { _fakeSoruce = value; OnPropertyChanged("FakeSource"); } } } private List
_source; public MainViewModel() { _currentPage = 1; _pageSize = 20; FakeDatabase fake = new FakeDatabase(); _source = fake.GenerateFakeSource(); _totalPage = _source.Count / _pageSize; _fakeSoruce = new ObservableCollection
(); List
result = _source.Take(20).ToList(); _fakeSoruce.Clear(); _fakeSoruce.AddRange(result); _firstPageCommand = new DelegateCommand(FirstPageAction); _previousPageCommand = new DelegateCommand(PreviousPageAction); _nextPageCommand = new DelegateCommand(NextPageAction); _lastPageCommand = new DelegateCommand(LastPageAction); } private void FirstPageAction() { CurrentPage = 1; var result = _source.Take(_pageSize).ToList(); _fakeSoruce.Clear(); _fakeSoruce.AddRange(result); } private void PreviousPageAction() { if(CurrentPage == 1) { return; } List
result = new List
(); if(CurrentPage == 2) { result = _source.Take(_pageSize).ToList(); } else { result = _source.Skip((CurrentPage - 2) * _pageSize).Take(_pageSize).ToList(); } _fakeSoruce.Clear(); _fakeSoruce.AddRange(result); CurrentPage--; } private void NextPageAction() { if(CurrentPage == _totalPage) { return; } List
result = new List
(); result = _source.Skip(CurrentPage * _pageSize).Take(_pageSize).ToList(); _fakeSoruce.Clear(); _fakeSoruce.AddRange(result); CurrentPage++; } private void LastPageAction() { CurrentPage = TotalPage; int skipCount = (_totalPage - 1) * _pageSize; int takeCount = _source.Count - skipCount; var result = _source.Skip(skipCount).Take(takeCount).ToList(); _fakeSoruce.Clear(); _fakeSoruce.AddRange(result); } }
View Code

绑定到UI的数据源只是需要显示的数据,不会把所有数据都取出来。当选择显示页数时,只需要将新的数据源附上即可。

总结:如果需要对该分页控件进行扩展,例如,增加每页显示条数功能,只需要在Pager控件中增加相应的依赖属性即可。

感谢您的阅读,代码点击下载。

posted on
2018-11-12 14:42 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/9946366.html

你可能感兴趣的文章
转载:mysql数据库密码忘记找回方法
查看>>
scratch少儿编程第一季——06、人在江湖混,没有背景怎么行。
查看>>
【贪心+DFS】D. Field expansion
查看>>
C# Async与Await的使用
查看>>
Mysql性能调优
查看>>
iOS基础-UIKit框架-多控制器管理-实例:qq界面框架
查看>>
IOS-每个程序员的编程之路上都应该看这11本书
查看>>
自定义tabbar(纯代码)
查看>>
小程序底部导航栏
查看>>
ibatis学习笔记
查看>>
18-ES6(1)
查看>>
poj1611 简单并查集
查看>>
Ubuntu 14.04下安装CUDA8.0
查看>>
跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
查看>>
C# BS消息推送 SignalR介绍(一)
查看>>
WPF星空效果
查看>>
WPF Layout 系统概述——Arrange
查看>>
PIGOSS
查看>>
几款Http小服务器
查看>>
iOS 数组排序
查看>>