WPF定制实现自己的分页控件并配合DataGrid使用
文章目录
- 效果预览
- 实现功能
- 属性说明
- 事件说明
- 相关代码
- 按钮事件参数:PageButtonEventArgs.cs
- 分页控件:DakaPathPageControl.xaml
- 使用
- Tip
因为项目需求,在使用WPF原生DataGrid时,需要使用到分页控件,所以自定义了分页控件
效果预览
实现功能
- 每页加载数据量选择,
PageSize
属性 - 根据输入页码进行跳转
GO
、上一页
、下一页
、刷新
四个按钮可以自定义单击事件- 可以设定边框、圆角边框
- 可以隐藏不想要显示的子控件
属性说明
属性 | 说明 |
---|---|
PageIndexVisibility |
控制[第 x 页] 文本框的显示
|
PageCountVisibility |
控制[共 x 页] 文本框的显示
|
DataCountVisibility |
控制[共 x 条记录] 文本框的显示
|
PageNumSelectVisibility |
控制每页显示数量 选择框的显示
|
JumpVisibility |
控制与页面跳转 相关控件的显示
|
PageIndex | 当前页码值 |
PageSize | 只读,每页显示数量 |
PageCount | 共多少页 |
DataCount | 共多少条记录 |
JumpNum | 跳转到的页码值 |
ButtonBackground | 设定按钮背景色 |
ButtonTextForeground | 设定按钮文字颜色 |
BorderBrush | 设定控件边框色 |
BorderThickness | 设定控件边框 |
Background | 设定控件背景色 |
CornerRadius | 设定控件边框圆角值 |
事件说明
事件 | 说明 |
---|---|
GoButtonOnClick |
GO 按钮绑定事件
|
PreviousButtonOnClick |
上一页 按钮绑定事件
|
NextButtonOnClick |
下一页 按钮绑定事件
|
RefreshButtonOnClick |
刷新 按钮绑定事件
|
相关代码
按钮事件参数:PageButtonEventArgs.cs
using System.Windows;namespace DakaPath.Wpf.Control.EventArgs
{public class PageButtonEventArgs : RoutedEventArgs{public PageButtonEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source){}public int PageIndex{set;get;}}
}
分页控件:DakaPathPageControl.xaml
新建
一个用户控件,并命名为DakaPathPageControl
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using DakaPath.Wpf.Control.EventArgs;namespace DakaPath.Wpf.Control
{/// <summary>/// DakaPathPageControl.xaml 的交互逻辑/// </summary>public partial class DakaPathPageControl : UserControl{public DakaPathPageControl(){InitializeComponent();}public Visibility PageIndexVisibility{get { return (Visibility)GetValue(PageIndexVisibilityProperty); }set { SetValue(PageIndexVisibilityProperty, value); }}public static readonly DependencyProperty PageIndexVisibilityProperty =DependencyProperty.Register("PageIndexVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public Visibility PageCountVisibility{get { return (Visibility)GetValue(PageCountVisibilityProperty); }set { SetValue(PageCountVisibilityProperty, value); }}public static readonly DependencyProperty PageCountVisibilityProperty =DependencyProperty.Register("PageCountVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public Visibility PageNumSelectVisibility{get { return (Visibility)GetValue(PageNumSelectVisibilityProperty); }set { SetValue(PageNumSelectVisibilityProperty, value); }}public static readonly DependencyProperty PageNumSelectVisibilityProperty =DependencyProperty.Register("PageNumSelectVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public Visibility DataCountVisibility{get { return (Visibility)GetValue(DataCountVisibilityProperty); }set { SetValue(DataCountVisibilityProperty, value); }}public static readonly DependencyProperty DataCountVisibilityProperty =DependencyProperty.Register("DataCountVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public Visibility JumpVisibility{get { return (Visibility)GetValue(JumpVisibilityProperty); }set { SetValue(JumpVisibilityProperty, value); }}public static readonly DependencyProperty JumpVisibilityProperty =DependencyProperty.Register("JumpVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public int PageIndex{get { return (int)GetValue(PageIndexProperty); }set { SetValue(PageIndexProperty, value); }}public static readonly DependencyProperty PageIndexProperty =DependencyProperty.Register("PageIndex", typeof(int), typeof(DakaPathPageControl), new PropertyMetadata(0));public int PageSize{get { return (int)GetValue(PageSizeProperty); }//set { SetValue(PageSizeProperty, value); }}public static readonly DependencyProperty PageSizeProperty =DependencyProperty.Register("PageSize", typeof(int), typeof(DakaPathPageControl), new PropertyMetadata(50));public int PageCount{get { return (int)GetValue(PageCountProperty); }set { SetValue(PageCountProperty, value); }}public static readonly DependencyProperty PageCountProperty =DependencyProperty.Register("PageCount", typeof(int), typeof(DakaPathPageControl), new PropertyMetadata(0));public int DataCount{get { return (int)GetValue(DataCountProperty); }set{SetValue(DataCountProperty, value);if (value > 0){SetValue(DataCountProperty, value);}else{SetValue(DataCountProperty, 0);}}}public static readonly DependencyProperty DataCountProperty =DependencyProperty.Register("DataCount", typeof(int), typeof(DakaPathPageControl), new PropertyMetadata(0));public string JumpNum{get{return (string)GetValue(JumpNumProperty);}set{SetValue(JumpNumProperty, value);}}public static readonly DependencyProperty JumpNumProperty =DependencyProperty.Register("JumpNum", typeof(string), typeof(DakaPathPageControl), null);public Brush ButtonBackground{get { return (Brush)GetValue(ButtonBackgroundProperty); }set { SetValue(ButtonBackgroundProperty, value); }}public static readonly DependencyProperty ButtonBackgroundProperty =DependencyProperty.Register("ButtonBackground", typeof(Brush), typeof(DakaPathPageControl), new PropertyMetadata((Brush)(new BrushConverter()).ConvertFromString("#337AB7")));public Brush ButtonTextForeground{get { return (Brush)GetValue(ButtonTextForegroundProperty); }set { SetValue(ButtonTextForegroundProperty, value); }}public static readonly DependencyProperty ButtonTextForegroundProperty =DependencyProperty.Register("ButtonTextForeground", typeof(Brush), typeof(DakaPathPageControl), new PropertyMetadata(Brushes.White));public new Brush BorderBrush{get { return (Brush)GetValue(BorderBrushProperty); }set { SetValue(BorderBrushProperty, value); }}public static readonly new DependencyProperty BorderBrushProperty =DependencyProperty.Register("BorderBrush", typeof(Brush), typeof(DakaPathPageControl));public new Brush Background{get { return (Brush)GetValue(BackgroundProperty); }set { SetValue(BackgroundProperty, value); }}public static readonly new DependencyProperty BackgroundProperty =DependencyProperty.Register("Background", typeof(Brush), typeof(DakaPathPageControl));public new Thickness BorderThickness{get { return (Thickness)GetValue(BorderThicknessProperty); }set { SetValue(BorderThicknessProperty, value); }}public static readonly new DependencyProperty BorderThicknessProperty =DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(DakaPathPageControl));public float CornerRadius{get { return (float)GetValue(CornerRadiusProperty); }set { SetValue(CornerRadiusProperty, value); }}public static readonly DependencyProperty CornerRadiusProperty =DependencyProperty.Register("CornerRadius", typeof(float), typeof(DakaPathPageControl));public event RoutedEventHandler NextPageButtonOnClick{add{this.AddHandler(NextPageButtonOnClickEvent, value);}remove { this.RemoveHandler(NextPageButtonOnClickEvent, value); }}public static readonly RoutedEvent NextPageButtonOnClickEvent = EventManager.RegisterRoutedEvent("NextButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DakaPathPageControl));private void btn_next_Click(object sender, RoutedEventArgs e){PageButtonEventArgs args = new PageButtonEventArgs(NextPageButtonOnClickEvent, this);if (PageCount < 0){PageCount = 0;}if (PageIndex < PageCount){PageIndex++;}else{PageIndex = PageCount;}args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}public event RoutedEventHandler PreviousPageButtonOnClick{add{this.AddHandler(PreviousPageButtonOnClickEvent, value);}remove { this.RemoveHandler(PreviousPageButtonOnClickEvent, value); }}public static readonly RoutedEvent PreviousPageButtonOnClickEvent =EventManager.RegisterRoutedEvent("PreviousButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DakaPathPageControl));private void btn_previous_Click(object sender, RoutedEventArgs e){PageButtonEventArgs args = new PageButtonEventArgs(PreviousPageButtonOnClickEvent, this);if (PageIndex > 0){PageIndex--;}args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}public event RoutedEventHandler GoPageButtonOnClick{add{this.AddHandler(GoPageButtonOnClickEvent, value);}remove { this.RemoveHandler(GoPageButtonOnClickEvent, value); }}public static readonly RoutedEvent GoPageButtonOnClickEvent =EventManager.RegisterRoutedEvent("GoButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DakaPathPageControl));private void btn_go_Click(object sender, RoutedEventArgs e){PageButtonEventArgs args = new PageButtonEventArgs(GoPageButtonOnClickEvent, this);int inputPageIndex = 0;int.TryParse(JumpNum,out inputPageIndex);if (PageCount < 0){PageCount = 0;}if (inputPageIndex <= PageCount && inputPageIndex > 0){PageIndex = inputPageIndex;}args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}public event RoutedEventHandler RefreshPageButtonOnClick{add{this.AddHandler(RefreshPageButtonOnClickEvent, value);}remove { this.RemoveHandler(RefreshPageButtonOnClickEvent, value); }}public static readonly RoutedEvent RefreshPageButtonOnClickEvent =EventManager.RegisterRoutedEvent("RefreshButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DakaPathPageControl));private void btn_refresh_Click(object sender, RoutedEventArgs e){PageButtonEventArgs args = new PageButtonEventArgs(RefreshPageButtonOnClickEvent, this);args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}private void cmb_pagesizeset_SelectionChanged(object sender, SelectionChangedEventArgs e){var cbi = e.AddedItems[0] as ComboBoxItem;if (cbi != null){int size = Int32.Parse(cbi.Content as String);SetValue(PageSizeProperty, size);PageButtonEventArgs args = new PageButtonEventArgs(RefreshPageButtonOnClickEvent, this);args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}}}
}
使用
将自己的控件项目
生成为dll文件
,并在需要使用分页控件的项目中引用该dll文件。接下来在需要使用分页控件的页面引用自己的控件库,即可在页面设计器中实时预览该控件的使用效果。
配合MahApps.Metro
,使用效果如下
Tip
自己注册属性,可以通过输入propdp
,然后按两次Tab键
自动生成相关代码
WPF定制实现自己的分页控件并配合DataGrid使用相关推荐
- WPF 分页控件应用
效果图: 前台代码: <UserControl x:Class="Layout.UI.Comm.Pager"xmlns="http://schemas.mic ...
- WPF 实现 DataGrid/ListView 分页控件
原文:WPF 实现 DataGrid/ListView 分页控件 在WPF中,通常会选用DataGrid/ListView进行数据展示,如果数据量不多,可以直接一个页面显示出来.如果数据量很大,200 ...
- WPF 分页控件的简单实现
想做个分页控件,想了想逻辑实现太复杂了,这不,用奇怪的方式实现了它,就如这张图一样... 看看效果: 下面就直接粘代码喽: 新建一个Pagination类: using System; using S ...
- WPF学习:分页控件
好多年没有发博了,今天闲暇,把给公司做的ERP分页控件拿出来,有很多都用到分页的,所以就封装了一个UserControl分页控件,效果还不错,可以看看,先! 注意:有用到存储过程. 如果还不明白, ...
- AspNetPager分页控件
http://files.cnblogs.com/zhangweiguo3984/AspNetPager433.rar 分页是Web应用程序中最常用到的功能之一,在ASP.NET中,虽然自带了一个可以 ...
- Repeater使用 AspNetPager分页控件
一.AspNetPager分页控件 分页是Web应用程序中最常用到的功能之一,在ASP.NET中,虽然自带了一个可以分页的DataGrid(asp.net 1.1)和GridView(asp.net ...
- WPF:从WPF Diagram Designer Part 1学习控件模板、移动、改变大小和旋转
欢迎转载,转载请注明:转载自周金根 [ http://zhoujg.cnblogs.com/ ] 由于上周主要做了项目组产品架构.给公司新员工培训以及其他会议等事情,在OpenExpressApp对建 ...
- 【开源】我的分页控件正式命名为QuickPager ASP.NET2.0分页控件
分页控件正式命名为 QuickPager ASP.NET2.0分页控件 . 版本号:2.0.0.1 Framework:.net2.0 分页方式:PostBack .URL (暂时没有实现URL的分页 ...
- 花两个小时,做了个分页控件
一个简单的分页控件,内置4种显示风格,主要是将AspNetForums的分页跟一个朋友(小熊)的合一块了,ANF的单调了点,小熊的吧,用起来有点麻烦,我人懒,就整一块了.:) 内置四种风格,通过设置S ...
- Winform分页控件之纯分页显示处理
在之前介绍的Winform分页控件中,都以分页控件+显示表格控件作为一个整体性的控件,不可分开,这样做的目的是可以实现更多的操作,集成更多丰富的特性,减少我们开发的工作量,这种情况虽然适用于大多数的情 ...
最新文章
- Java线程的状态转换
- 【git】Git 提示fatal: remote origin already exists 错误解决办法
- Mac实时远程抓Ubuntu的网络包
- row_number()函数的使用
- spine 破解 闪退打不开
- EdrawMax 11 for mac(亿图图示)中文版
- 周立功ZCANPRO基础使用教程
- 线性代数学习笔记——第三十三讲——向量混合积的几何意义
- 超级经典的Word技巧
- thinkPHP+jQuery实现站内信功能-讲给菜鸟同学
- 关于2048小游戏的开发感想
- SSL协议和SET协议
- rhel7 http实例
- 实验一 R语言的基本操作和数据组织
- 计算机科学与技术考研报名属于哪一类,计算机在考研中属于什么类别
- 最近朋友民间借贷起诉,聊天记录内容过多,聊天长截图需要处理成A4纸上,方便打印
- FFmpeg的软、硬解码方式梳理
- C语言编写仓库管理系统
- unity3d 理解刚体(Rigidbody)和碰撞体(Collider)以及触发器(Is Trigger),边学边更新
- php习题,PHP程序设计试题与答案
热门文章
- Spring中AOP的Introductions使用介绍(五)
- 传智java完整教学视频
- ElasticsearchTemplate的详细使用,完成多条件查询、匹配度查询等
- 微信扫带参数二维码,微信回调
- nginxlocation打印自定义日志
- Pytorch中pack_padded_sequence和pad_packed_sequence的理解
- linux yum 安装播放器,centos5下用yum安装MPlayer播放器
- META-INF、WEB-INF分别是什么?
- word文档docx解密方法,word文档docx不能复制打印怎么办?
- 什么是Windows驱动程序?