WPF里DataGrid分页控件
1.主要代码:
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command; namespace PaggingDataGrid.CustomCtrl
{
/// <summary>按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 元素中: xmlns:MyNamespace="clr-namespace:PaggingDataGrid" 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 元素中: xmlns:MyNamespace="clr-namespace:PaggingDataGrid;assembly=PaggingDataGrid" 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用, 并重新生成以避免编译错误: 在解决方案资源管理器中右击目标项目,然后依次单击 “添加引用”->“项目”->[浏览查找并选择此项目] 步骤 2) 继续操作并在 XAML 文件中使用控件。
/// <MyNamespace:PaggingControl />
/// </summary>
public class PaggingControl : Control
{
// Using a DependencyProperty as the backing store for TotalPages. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TotalPagesProperty =
DependencyProperty.Register("TotalPages", typeof (uint), typeof (PaggingControl),
new PropertyMetadata(1u, TotalPagesPropertyChangeCallback)); // Using a DependencyProperty as the backing store for CurrentPage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CurrentPageProperty =
DependencyProperty.Register("CurrentPage", typeof (uint), typeof (PaggingControl),
new PropertyMetadata(1u, CurrentPagePropertyChangeCallback)); // Using a DependencyProperty as the backing store for PageSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageSizeProperty =
DependencyProperty.Register("PageSize", typeof (uint), typeof (PaggingControl),
new PropertyMetadata(10u, PageSizePropertyChangecallback)); // Using a DependencyProperty as the backing store for PageSizeList. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageSizeListProperty =
DependencyProperty.Register("PageSizeList", typeof (ObservableCollection<uint>), typeof (PaggingControl),
new PropertyMetadata(new ObservableCollection<uint> {10u, 20u, 50u, 100u})); // Using a DependencyProperty as the backing store for ItemsCount. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemsCountProperty =
DependencyProperty.Register("ItemsCount", typeof (uint), typeof (PaggingControl),
new PropertyMetadata(1u, ItemsCountPropertyChangeCallback)); // Using a DependencyProperty as the backing store for PageRefreshCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageRefreshCommandProperty =
DependencyProperty.Register("PageRefreshCommand", typeof (ICommand), typeof (PaggingControl),
new PropertyMetadata(null)); static PaggingControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof (PaggingControl),
new FrameworkPropertyMetadata(typeof (PaggingControl)));
} public PaggingControl()
{
//第一次时候 刷新一下列表
Loaded += delegate { RaisePageRefreshEvent(); };
} /// <summary>总页数</summary>
public uint TotalPages
{
get { return (uint) GetValue(TotalPagesProperty); }
set { SetValue(TotalPagesProperty, value); }
} /// <summary>当前页</summary>
public uint CurrentPage
{
get { return (uint) GetValue(CurrentPageProperty); }
set { SetValue(CurrentPageProperty, value); }
} /// <summary>每页的大小</summary>
public uint PageSize
{
get { return (uint) GetValue(PageSizeProperty); }
set { SetValue(PageSizeProperty, value); }
} /// <summary>每页大小列表,即页面大小选择框的数据源</summary>
public ObservableCollection<uint> PageSizeList
{
get { return (ObservableCollection<uint>) GetValue(PageSizeListProperty); }
set { SetValue(PageSizeListProperty, value); }
} /// <summary>转到首页</summary>
public ICommand FirstPageCmd
{
get
{
return new RelayCommand(() => { CurrentPage = ; }
, () => CurrentPage != );
}
} /// <summary>上一页</summary>
public ICommand PreviousPageCmd
{
get { return new RelayCommand(() => { CurrentPage -= ; }, () => CurrentPage > ); }
} /// <summary>下一页</summary>
public ICommand NextPageCmd
{
get { return new RelayCommand(() => { CurrentPage += ; }, () => CurrentPage < TotalPages); }
} /// <summary>尾页</summary>
public ICommand LastPageCmd
{
get { return new RelayCommand(() => { CurrentPage = TotalPages; }, () => CurrentPage != TotalPages); }
} /// <summary>转到某一页</summary>
public ICommand TurnToPageCmd
{
get { return new RelayCommand(RaisePageRefreshEvent); }
} /// <summary>数据总大小</summary>
public uint ItemsCount
{
get { return (uint) GetValue(ItemsCountProperty); }
set { SetValue(ItemsCountProperty, value); }
} /// <summary>参数 选择Tuple(uint, uint)代表页数 和页大小,即index和length</summary>
public ICommand PageRefreshCommand
{
get { return (ICommand) GetValue(PageRefreshCommandProperty); }
set { SetValue(PageRefreshCommandProperty, value); }
} private static void TotalPagesPropertyChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = d as PaggingControl;
if (ctrl != null)
{
if (ctrl.CurrentPage > ctrl.TotalPages)
{
ctrl.CurrentPage = ctrl.TotalPages;
}
else if (ctrl.CurrentPage <= )
{
ctrl.CurrentPage = ;
}
ctrl.RaisePageRefreshEvent();
}
} private static void CurrentPagePropertyChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = d as PaggingControl;
if (ctrl != null)
{
if (ctrl.CurrentPage > ctrl.TotalPages)
{
ctrl.CurrentPage = ctrl.TotalPages;
}
else if (ctrl.CurrentPage <= )
{
ctrl.CurrentPage = ;
}
ctrl.RaisePageRefreshEvent();
}
} private static void PageSizePropertyChangecallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = d as PaggingControl;
if (ctrl != null)
{
ctrl.TotalPages = ctrl.ItemsCount/ctrl.PageSize + (ctrl.ItemsCount%ctrl.PageSize == ? : 1u);
ctrl.RaisePageRefreshEvent();
}
} private static void ItemsCountPropertyChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = d as PaggingControl;
if (ctrl != null)
{
ctrl.TotalPages = ctrl.ItemsCount/ctrl.PageSize + (ctrl.ItemsCount%ctrl.PageSize == ? : 1u);
}
} private void RaisePageRefreshEvent()
{
if (PageRefreshCommand != null)
{
PageRefreshCommand.Execute(Tuple.Create(CurrentPage, PageSize));
}
}
}
}
2.效果图:
外部接口最少,只需要外部两个接口即可实现分页的所有功能。

3.参考网页:
https://www.codeproject.com/Articles/350447/WPF-Paging-in-DataGrid-ListBox
将其代码进行了改进,忽略了不需要的部分,加入了一些简单的特性。更符合MVVM思想。
4.源码下载
https://files.cnblogs.com/files/chlm/PaggingDataGrid.rar
WPF里DataGrid分页控件的更多相关文章
- WPF自定义DataGrid分页控件
新建Custom Control,名:PagingDataGrid 打开工程下面的Themes\Generic.xaml xaml里面代码替换如下 <Style x:Key="{x:T ...
- WPF管理系统自定义分页控件 - WPF特工队内部资料
最近做一个演示的管理系统项目,需要用到分页控件,在网上找了很多,依然找到与UI模版匹配的,最后干脆自己写一个. 分页控件分析: 1.分页控件分简单显示和复杂显示两种: 2.包含上一页.下一页以及页码明 ...
- WPF简单的分页控件实现
XAML代码(使用ItemsControl控件实现): <UserControl x:Class="SunCreate.Vipf.Client.UI.CityDoor.PageCont ...
- ajax 分页控件,基于jquery
/* 分页插件,依赖jQuery库 version: 1.1.0 author: Harrison Cao release date: 2013-09-23 相对 v1.0版本 修正了分页居中 使用方 ...
- WPF 实现 DataGrid/ListView 分页控件
在WPF中,通常会选用DataGrid/ListView进行数据展示,如果数据量不多,可以直接一个页面显示出来.如果数据量很大,2000条数据,一次性显示在一个页面中,不仅消耗资源,而且用户体验也很糟 ...
- 两款不同应用场景的Wpf分页控件
简介 今天给大家分享两个Wpf分页控件,本篇博客主要介绍一些实现思路和使用方法,具体实现和应用代码请参考文末的Demo链接 废话不多说,先看一下效果~ (两款控件显示效果是一样的) 实现思路 一款控件 ...
- WPF 分页控件 WPF 多线程 BackgroundWorker
WPF 分页控件 WPF 多线程 BackgroundWorker 大家好,好久没有发表一篇像样的博客了,最近的开发实在头疼,很多东西无从下口,需求没完没了,更要命的是公司的开发从来不走正规流程啊, ...
- WPF 分页控件的实现 -用户控件
效果图:
- WPF自定义分页控件,样式自定义,简单易用
WPF自定义分页控件 做了许久伸手党,终于有机会贡献一波,搜索一下WPF分页控件,还是多,但是不太通用,主要就是样式问题,这个WPF很好解决,还有一个就是分页控件嘛,只关心几个数字的变动就行了,把页码 ...
随机推荐
- android通用適配器
一.需求分析 在寻常的android开发过程中.ListView.GridView适配的编写是一件非常麻烦并且非常反复的事情,每次都须要考虑性能的优化.item的编写.获取网络图片时候信息的错乱等问题 ...
- gen_server笔记
http://www.ask3.cn/a/jingcaibowen/tech/Erlang/2013/0614/42043.html gen_server是erlang的OTP框架中最常用的“行为模式 ...
- 关于iPhone开发的一些建议
建议 以后的应用程序,都使用AutoLayout, 不要再用绝对定位CGReck. 使用类似网页的方式来设计界面. 设计师好,程序员也好,尽量使用点这个单位进行思考,而不要使用像素.比如,你需要做44 ...
- 简单易用的动画animations
_tableView_selc.frame=CGRectMake(20, , 20,20); [UIView animateWithDuration:0.3f animations:^{ _table ...
- 【BZOJ 1019】 [SHOI2008]汉诺塔
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1019 [题意] [题解] 这个题解讲得很清楚了 http://blog.sina.co ...
- oracle的sql查询结果拼接
oracle数据库中,使用wm_concat(column)函数,可以进行字段合并 oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oracle wm_co ...
- 《⑨也懂系列:MinGW-w64安装教程》著名C/C++编译器GCC的Windows版本(MinGW-w64在安装的时候可以选择版本,有图,一步一步)
发布日期 2016年10月31日 分类 教程 标签 编程.软件 前言<⑨也懂系列:MinGW-w64安装教程>这篇文章由 rsreland (http://rsreland.net)于 2 ...
- 如何将字段中带逗号的SQLite数据库数据导入到MySQL
以前在数据库导入中没有遇到过什么问题,如下这样导入 load data local infile 'D:\data.csv' into table table1 fields terminated b ...
- Material Design: NavigationView FlaotingActionBar SnackBar采用
转载 请明确说明 MingsangAndroid 本文介绍了Design Support Library的引入 拥抱Android Design Support Library新变化(导航视图.悬浮A ...
- gdal1.10编译经验
作者:朱金灿 来源:http://blog.csdn.net/clever101 集成了一些扩展库,使用makefile编译,出现了一个链接错误: libcmt.lib(crt0.obj): erro ...