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 类自定义编辑 ...
随机推荐
- github远程仓库初始化配置
github远程仓库的提交一般是通过shell进行,windows下有Git Bash工具(https://git-for-windows.github.io/) 由于本地Git仓库和GitHub仓库 ...
- SLIC superpixel算法
标题 SLIC superpixel算法 作者 YangZheng 联系方式 263693992 SLIC算法是simple linear iterative cluster的简称,该算法用来生成超像 ...
- C#多线程编程(7)--锁
一提到线程同步,就会提到锁,作为线程同步的手段之一,锁总是饱受质疑.一方面锁的使用很简单,只要在代码不想被重入的地方(多个线程同时执行的地方)加上锁,就可以保证无论何时,该段代码最多有一个线程在执行: ...
- u-boot的SPL源码流程分析
上次梳理了一下SPL的基本概念和代码总体思路,这次就针对代码跑的流程做个梳理.SPL中,入口在u-boot-spl.lds中 ENTRY(_start) SECTIONS { .text : { __ ...
- Mac HomeBrew 常用命令
mac 系统常用的软件安装工具就是 homebrew, 其最常用的命令如下: 安装(需要 Ruby):ruby -e "$(curl -fsSL https://raw.github.com ...
- zabbix监控redis性能
创建采集脚本 mkdir -p /etc/zabbix/scripts chown -R zabbix.root /etc/zabbix/scripts vim redis_status.sh #! ...
- Python 中列表生成式和生成器
列表生成式 即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 举个例子,要生成list [1,2,3,4,5,6,7,8,9,10]可以用l ...
- VUE请求本地数据的配置json-server
VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...
- Sublime Text、webstorm等编译器快速编写HTML/CSS代码的技巧
Sublime Text.webstorm等编译器,如果你从事Web前端开发的话,对这几款软件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演 ...
- 获取服务器时间js代码
function getSevertime(){ var xmlHttp = new XMLHttpRequest(); if( !xmlHttp ){ xmlHttp = new ActiveXOb ...