wpf研究之道——datagrid控件分页
datagrid控件分页效果,如下图所示:

上一页,下一页,可以跳到任何一页。当页码比较多的时候,只显示几页,其余用点点,界面实现如下:
<!--分页-->
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="" Margin="0 20" x:Name="fulltextPager"> <Button x:Name="prePage" Click="prePage_Click" Style="{StaticResource btnPager}" ToolTip="上一页"/> <Button Style="{StaticResource btnPager}" Content="" x:Name="bntGoFirstPage" Click="bntGoFirstPage_Click" /> <TextBlock x:Name="predot" Text="..." Visibility="{Binding PreVisible}"/> <ItemsControl ItemsSource="{Binding Pages}" x:Name="btnPagerContainer">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<Button Style="{StaticResource btnPager}" Content="{Binding Name}" Click="btn_GotoPage" />
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<!--这里用WrapPanel 当容器放Button-->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate> <WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> <TextBlock x:Name="nextdot" Text="..." Visibility="{Binding NextVisible}"/> <Button Style="{StaticResource btnPager}" Content="{Binding Total}" x:Name="btnGoLastPage" Click="btnGoLastPage_Click" /> <Button x:Name="nextPage" Click="nextPage_Click" Content=">>" Style="{StaticResource btnPager}" ToolTip="下一页"/> <TextBlock Text="当前"/>
<TextBlock Text="{Binding PageIndex}" Foreground="#3091f2"/>
<TextBlock Text="页"/> <TextBlock Text="跳转到" Style="{StaticResource pagerStyle}" Margin="5 0 5 0"/>
<TextBox x:Name="wantToGo" Width="" Height=""></TextBox>
<TextBlock Text="页" Style="{StaticResource pagerStyle}"/> <TextBlock Style="{StaticResource pagerStyle}"> <Button Content="go" x:Name="goPage" Click="goPage_Click" Style="{StaticResource btnPager}" /> </TextBlock> <TextBlock Style="{StaticResource pagerStyle}"> <TextBlock Text="共"/>
<TextBlock Text="{Binding ItemCount}" Foreground="#3091f2"/>
<TextBlock Text="条"/> </TextBlock> </StackPanel>
ItemsControl 是一个完全自定义的集合控件,它没有默认的形状,不像button,它默认为长方形。看看它的数据是如何绑定的?

this 代表了TestCaseUserControl ,这是个自定义的用户控件,它里面包含了datagrid控件以及分页。Data是个复杂的对象,包含了数据源以及一些分页支持。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading; namespace WpfReovlveTest
{
public class PageDataManager<T> : INotifyPropertyChanged
{
private int pageSize = ;
public int PageSize
{
get { return pageSize; }
set
{
pageSize = value;
NotifyPropertyChanged("PageSize");
}
} private int pageIndex;
public int PageIndex
{
get { return pageIndex; }
set
{
pageIndex = value;
NotifyPropertyChanged("PageIndex");
}
} private int total;
public int Total
{
get { return total; }
set
{
total = value;
NotifyPropertyChanged("Total");
}
} private Visibility preVisible = Visibility.Collapsed; public Visibility PreVisible
{
get { return preVisible; }
set
{
preVisible = value;
NotifyPropertyChanged("PreVisible");
}
} private Visibility nextVisible = Visibility.Collapsed; public Visibility NextVisible
{
get { return nextVisible; }
set
{
nextVisible = value;
NotifyPropertyChanged("NextVisible");
}
} private ObservableCollection<Pages> pages;
public ObservableCollection<Pages> Pages
{
get { return pages; }
set
{
pages = value;
NotifyPropertyChanged("Pages");
}
}
/// <summary>
/// 总数
/// </summary>
private int itemCount;
public int ItemCount
{
get { return itemCount; }
set
{
itemCount = value;
NotifyPropertyChanged("ItemCount");
}
} private ObservableCollection<T> dataSource; /// <summary>
/// 总的数据源
/// </summary>
public ObservableCollection<T> DataSource
{
get { return dataSource; }
set
{
dataSource = value;
NotifyPropertyChanged("DataSource");
}
} private ObservableCollection<T> pagerSource = new ObservableCollection<T>(); /// <summary>
/// 每页的数据源
/// </summary>
public ObservableCollection<T> PagerSource
{
get { return pagerSource; }
set
{
pagerSource = value;
NotifyPropertyChanged("PagerSource");
}
} public Action<int, int> PagerOp; public bool IsMemoryPager { set; get; } //负责监视属性的变化
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string Propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(Propertyname));
}
} /// <summary>
/// 打开等待窗口
/// </summary>
public Action OpenWaitingWindow { set; get; }
/// <summary>
/// 关闭等待窗口
/// </summary>
public Action CloseWaitingWindow { set; get; } public UIElement Owner { get; set; } public PageDataManager(ObservableCollection<T> source, int count, bool isMemoryPager = true, Action<int, int> PagerOp = null, int pageSize = , int pageIndex = )
{
this.PageSize = pageSize;
this.DataSource = source;
this.ItemCount = count;
this.Total = this.ItemCount % PageSize == ? ItemCount / PageSize : ItemCount / PageSize + ; this.PagerOp = PagerOp; this.PageIndex = pageIndex; this.IsMemoryPager = isMemoryPager; Pager(this.PageIndex, false);
} private void MakePagerNum()
{
//初始化页数数组
if (this.Pages == null)
{
this.Pages = new ObservableCollection<Pages>();
}
else
{
this.Pages.Clear();
} this.PreVisible = Visibility.Collapsed;
this.NextVisible = Visibility.Collapsed; if (this.Total > )
{
//以当前页为分界点,向左借2个,向右借2个 int leftLength = this.PageIndex - ;
int rightLength = this.Total - this.PageIndex; if (leftLength > && rightLength > )
{
this.PreVisible = Visibility.Visible; for (int i = PageIndex - ; i <= PageIndex + ; i++)
{
this.Pages.Add(new Pages() { Name = i.ToString(), PageIndex = i });
}
this.NextVisible = Visibility.Visible;
} if (rightLength <= )
{
//右边的不够,向左边借
this.PreVisible = Visibility.Visible; for (int i = this.PageIndex - ( - rightLength); i <= this.Total - ; i++)
{
this.Pages.Add(new Pages() { Name = i.ToString(), PageIndex = i });
}
}
if (leftLength <= )
{
//左边的不够,向右边借
for (int i = ; i <= this.PageIndex + ( - leftLength); i++)
{
this.Pages.Add(new Pages() { Name = i.ToString(), PageIndex = i });
}
this.NextVisible = Visibility.Visible;
}
}
else
{
for (int i = ; i <= Total - ; i++)
{
this.Pages.Add(new Pages() { Name = i.ToString(), PageIndex = i });
}
}
} private void PagerOpCompleted(IAsyncResult result)
{
try
{
var handler = (Action<int, int>)((AsyncResult)result).AsyncDelegate;
handler.EndInvoke(result); if (this.Owner != null)
{
this.Owner.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(ThreadStart)delegate()
{
FillPagerSource();
});
}
}
catch (Exception ex)
{
MessageBox.Show("异步分页出错:" + ex.Message);
}
finally
{
//关闭等待图标
if (this.Owner != null)
{
this.Owner.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(ThreadStart)delegate()
{
if (CloseWaitingWindow != null)
{
CloseWaitingWindow();
}
});
}
}
}
public void Pager(int pageIndex, bool canPager = true)
{
if (pageIndex < || pageIndex > this.Total)
{
return;
}
this.PageIndex = pageIndex; MakePagerNum(); if (PagerOp != null && canPager)
{
//委托异步执行 IAsyncResult result = PagerOp.BeginInvoke(this.PageSize, pageIndex, new AsyncCallback(PagerOpCompleted), null); //打开等待图标 if (OpenWaitingWindow != null)
{
OpenWaitingWindow();
}
}
else
{
FillPagerSource();
}
} private void FillPagerSource()
{
IEnumerable<T> pagerDatas = DataSource; if (this.IsMemoryPager)
{
List<T> tempSource = new List<T>();
tempSource.AddRange(this.DataSource);
pagerDatas = tempSource.Skip((this.PageIndex - ) * PageSize).Take(this.PageSize);
} this.PagerSource.Clear(); foreach (var item in pagerDatas)
{
this.PagerSource.Add(item);
}
}
}
} public class Pages
{
public string Name { get; set; }
public int PageIndex { get; set; }
}
分页类
这是一个通用的wpf分页类,即可用给datagrid使用,也可以给ListView使用。使用方法如下:
public void SetSource(ObservableCollection<TestCaseListViewModel> models, int itemCount, bool isMemoryPager, int pageIndex = )
{
this.FullTextList = models;
this.Data = new PageDataManager<TestCaseListViewModel>(FullTextList, itemCount, isMemoryPager, this.PagerFullTask, PageSize, pageIndex); this.Data.OpenWaitingWindow = OpenWaitingWindow;
this.Data.CloseWaitingWindow = CloseWaitingWindow;
this.Data.Owner = this; this.DataContext = Data;
this.TestCaseDataGrid.DataContext = Data.PagerSource; fulltextPager.Visibility = itemCount == ? Visibility.Collapsed : Visibility.Visible; prePage.Content = "<<";
btnGoLastPage.Visibility = Data.Total == ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible; }
在自定义的用户控件中,实例化分页类,参数说明如下:
FullTextList:数据源
itemCount:总数
isMemoryPager:是否内存分页(false:数据库分页)
PagerFullTask:获取每页数据源的方法
这个数据分页类与具体的控件无关,它只与数据源相关。
wpf研究之道——datagrid控件分页的更多相关文章
- wpf研究之道-datagrid控件(1)
"想要说些什么 又不知从何说起",每当想要写一些关于wpf的文章,总是沉思良久,怕自己写不好.今天我想要说的是wpf中datagrid控件.我们先来看看它在整个类的层次结构: ...
- wpf研究之道——datagrid控件数据绑定
前台: <DataGrid x:Name="TestCaseDataGrid" ItemsSource="{Binding}" > {binding ...
- wpf研究之道-grid控件
想要说些什么,却不知道从哪开始."形而上谓之道,形而下谓之器".与其坐而论道,不如脚踏实地,从最实用的地方开始. 我们先来看看wpf中的grid控件.grid控件是个网格的布局控件 ...
- WPF:获取DataGrid控件单元格DataGridCell
转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...
- WPF的DataGrid控件从excel里复制数据然后粘贴
WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...
- WPF DataGrid 控件的运用
WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...
- C# WPF DataGrid控件实现三级联动
利用DataGrid控件实现联动的功能,在数据库客户软件中是随处可见的,然而网上的资料却是少之又少,令人崩溃. 本篇博文将介绍利用DataGrid控件模板定义的三个ComboBox实现“省.市.区”的 ...
- WPF 4 DataGrid 控件(进阶篇一)
原文:WPF 4 DataGrid 控件(进阶篇一) 上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...
- WPF 4 DataGrid 控件(进阶篇二)
原文:WPF 4 DataGrid 控件(进阶篇二) 上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...
随机推荐
- Linux tar包安装Nginx-1.7.6 (yum方式安装依赖)
1.首先安装依赖包(依赖包有点多,我们采用yum的方式来安装) yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel ...
- session、cookie与“记住我的登录状态”的功能的实现
Cookie的机制 Cookie是浏览器(User Agent)访问一些网站后,这些网站存放在客户端的一组数据,用于使网站等跟踪用户,实现用户自定义功能. Cookie的Domain和Path属性标识 ...
- Python之list列表方法详解
# 列表test = [11, 22, 33, 44, 55, 22]# append方法,在test列表末尾增加一个元素# append(self, p_object) 参数:p_object:待添 ...
- 在foxmail和outlook中设置QQ邮箱、gmail邮箱、新浪邮箱、微软邮箱、网易邮箱等的方法
怎么用邮件客户端如outlook和foxmail来设置各种邮箱 很多人平时都是在网页上面收发邮件,这个很简单,不用其他的设置,不过在客户端上设置收发邮件还是很不错的,今天就来讲讲各种邮箱在outloo ...
- PHP开发程序员的学习路线
PHP开发程序员的学习路线 兄弟连PHP培训,简单为大家梳理了每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定学习成长目标. 第一阶段:基础阶段(基础PHP程序员) 重点:把LNMP搞熟 ...
- gdb命令调试技巧
gdb命令调试技巧 一.信息显示1.显示gdb版本 (gdb) show version2.显示gdb版权 (gdb) show version or show warranty3.启动时不显示提示信 ...
- Java使用对象流读取文件的问题
把对象进行持久化(把对象存到本地)要用到对象流进行处理,在用对象流处理过程中,在写对象和读对象时候遇到了很多问题,分享一下. 我们处理对象数据的时候不可能只处理一个对象,在系统运行的时候,可能产生的对 ...
- [Bzoj 2547] [Ctsc2002] 玩具兵
2547: [Ctsc2002]玩具兵 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 317 Solved: 152[Submit][Status] ...
- MongoDb进阶实践之二 如何在Windows上配置MongoDB
一.引言 上一篇文章,我介绍了如何在Linux系统上安装和配置MongoDB,其实都不是很难,不需要安装和编译,省去了Make && Make Install 命 ...
- Android学习笔记2——shape
Android有很多特别的xml文件,如常用的selector.style以及shape,熟练使用这些xml可以是我们的项目变得更个性化. 一.子标签(corners.gradient.padding ...