AI智能
改变未来

C# WPF 分页插件


一,前言

最近项目中开发一个小工具,使用的是C# ,用到分页这一块,也是一边学习一边做项目,但是对于分页而言在网上找到许多资料,说实话有点看不懂,感觉很复杂。不过资料中基本都是围绕触发器来写,自定义插件类,触发器类然后引入项目,感觉很复杂,于是就自己写一个。毕竟分页插件的核心也就是那几点,我将那些类合并成一个。不过自己写出来的扩展性不强,而且代码臃肿,只是实现了现在的需求,贴出来大家学习学习,也希望大佬看到后指点指点。,先看下效果图:需要说明一点,因为项目只是写个小的工具,所以数据存储没有使用数据库,是直接存在文件中的,数据都是从文件中抓取的

二,思路

分页重要属性:总页数,分页数据条数,当前页码,总条数

按钮的变化:我主要是通过flag真假标记来实现

三,代码

1,分页实体类,我是将按钮和分页属性合并在一起直接使用的,我这里只用到了8个按钮,如果需要更多可以自己添加,理论上是无限加长的

[code]using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;//分页数据书体类namespace Model{public class Pages{public int pages{ get; set; } //总页数public int count { get; set; } //每页显示条数public int sum { get; set; } //总条数//窗体页面引用四个页按钮,定义为button(1-4)public int button1 { get; set; }public int button2 { get; set; }public int button3 { get; set; }public int button4 { get; set; }public int currentPage { get; set; } //当前页public string text { get; set; }//文本信息public bool flag1 { get; set; }//判断是否为当前页的标记,对应按钮button1public bool flag2 { get; set; }//判断是否为当前页的标记,对应按钮button2public bool flag3 { get; set; }//判断是否为当前页的标记,对应按钮button3public bool flag4 { get; set; }//判断是否为当前页的标记,对应按钮button4public bool flag5{ get; set; }//判断是否为当前页的标记,对应按钮首页 定义值为 -10public bool flag6 { get; set; }//判断是否为当前页的标记,对应按钮尾页  定义值为 -1public bool flag7 { get; set; }//判断是否为当前页的标记,对应按钮上一页 定义值为 -5public bool flag8 { get; set; }//判断是否为当前页的标记,对应按钮下一页  定义值为 -6public bool flag9 { get; set; }//判断是否还有上一页public bool flag10 { get; set; }//判断是否还有下一页//判断页的范围区间public bool page_flag1 { get; set; }public bool page_flag2 { get; set; }public bool page_flag3 { get; set; }public bool page_flag4 { get; set; }public bool label_flag { get; set; }}}

2,xaml:定义好按钮格式

[code]      <Grid Grid.Row=\"3\" x:Name=\"pg\" Loaded=\"pg_Loaded\"><Grid.ColumnDefinitions><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><StackPanel Grid.Column=\"0\" Orientation=\"Horizontal\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\"><Button x:Name=\"first_button\" Click=\"first_button_Click\"><Button.Style><Style TargetType=\"{x:Type Button}\"><Setter Property=\"Width\" Value=\"40px\"/><Setter Property=\"Height\" Value=\"30px\"/><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"Content\" Value=\"首页\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Margin\" Value=\"10,0,0,0\"/><Setter Property=\"Visibility\" Value=\"Visible\"/><Setter Property=\"Tag\" Value=\"1\"></Setter><Setter Property=\"Template\"><Setter.Value><ControlTemplate><Border x:Name=\"boder\" BorderThickness=\"1,1,1,1\" BorderBrush=\"Black\" Background=\"White\"><TextBlock x:Name=\"text1\" Text=\"首页\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/></Border><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"BorderBrush\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/></Trigger><DataTrigger Binding=\"{Binding Path=currentPage}\" Value=\"1\"><Setter Property=\"Background\" Value=\"#FFD7D7D7\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"></Setter></DataTrigger><DataTrigger Binding=\"{Binding Path=flag5}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"></Setter></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Button.Style></Button><Button x:Name=\"up_button\" Click=\"up_button_Click\"><Button.Style><Style TargetType=\"{x:Type Button}\"><Setter Property=\"Width\" Value=\"40px\"/><Setter Property=\"Height\" Value=\"30px\"/><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"Content\" Value=\"&lt;&lt;\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Margin\" Value=\"10,0,0,0\"/><Setter Property=\"Visibility\" Value=\"Visible\"/><Setter Property=\"Tag\" Value=\"{Binding currentPage}\"></Setter><Setter Property=\"Template\"><Setter.Value><ControlTemplate><Border x:Name=\"boder\" BorderThickness=\"1,1,1,1\" BorderBrush=\"Black\" Background=\"White\"><TextBlock x:Name=\"text1\" Text=\"&lt;&lt;\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/></Border><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"BorderBrush\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/></Trigger><DataTrigger Binding=\"{Binding Path=flag9}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FFD7D7D7\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"/></DataTrigger><DataTrigger Binding=\"{Binding Path=flag7}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/></DataTrigger><DataTrigger Binding=\"{Binding Path=currentPage}\" Value=\"1\"><Setter Property=\"Background\" Value=\"#FFD7D7D7\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"></Setter></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Button.Style></Button><Button x:Name=\"pages_button1\" Click=\"pages_buttons_Click\"><Button.Style><Style TargetType=\"{x:Type Button}\"><Setter Property=\"Width\" Value=\"30px\"/><Setter Property=\"Height\" Value=\"30px\"/><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"Content\" Value=\"{Binding button1}\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Margin\" Value=\"10,0,0,0\"/><Setter Property=\"Visibility\" Value=\"Collapsed\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate><Border x:Name=\"boder\" BorderThickness=\"1,1,1,1\" BorderBrush=\"Black\" Background=\"White\"><TextBlock x:Name=\"text1\" Text=\"{Binding button1}\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/></Border><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"BorderBrush\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/></Trigger><DataTrigger Binding=\"{Binding Path=page_flag1}\" Value=\"true\"><Setter Property=\"Visibility\" Value=\"Visible\"/></DataTrigger><DataTrigger Binding=\"{Binding Path=flag1}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"></Setter></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Button.Style></Button><Button x:Name=\"pages_button2\"  Click=\"pages_buttons_Click\"><Button.Style><Style TargetType=\"{x:Type Button}\"><Setter Property=\"Width\" Value=\"30px\"/><Setter Property=\"Height\" Value=\"30px\"/><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Margin\" Value=\"10,0,0,0\"/><Setter Property=\"Content\" Value=\"{Binding button2}\"/><Setter Property=\"Visibility\" Value=\"Collapsed\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate><Border x:Name=\"boder\" BorderThickness=\"1,1,1,1\" BorderBrush=\"Black\" Background=\"White\"><TextBlock Text=\"{Binding button2}\" Name=\"text2\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/></Border><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"BorderBrush\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/></Trigger><DataTrigger Binding=\"{Binding Path=page_flag2}\" Value=\"true\"><Setter Property=\"Visibility\" Value=\"Visible\"/></DataTrigger><DataTrigger Binding=\"{Binding Path=flag2}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"></Setter></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Button.Style></Button><Label  Margin=\"10,0,0,0\"><Label.Style><Style TargetType=\"{x:Type Label}\"><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Visibility\" Value=\"Collapsed\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate><TextBlock Text=\"..\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/><ControlTemplate.Triggers><DataTrigger Binding=\"{Binding Path=label_flag}\" Value=\"true\"><Setter Property=\"Visibility\" Value=\"Visible\"/></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Label.Style></Label><Button x:Name=\"pages_button3\" Click=\"pages_buttons_Click\"><Button.Style><Style TargetType=\"{x:Type Button}\"><Setter Property=\"Width\" Value=\"30px\"/><Setter Property=\"Height\" Value=\"30px\"/><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Margin\" Value=\"10,0,0,0\"/><Setter Property=\"Content\" Value=\"{Binding button3}\"/><Setter Property=\"Visibility\" Value=\"Collapsed\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate><Border x:Name=\"boder\" BorderThickness=\"1,1,1,1\" BorderBrush=\"Black\" Background=\"White\"><TextBlock Text=\"{Binding button3}\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/></Border><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"BorderBrush\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/></Trigger><DataTrigger Binding=\"{Binding Path=page_flag3}\" Value=\"true\"><Setter Property=\"Visibility\" Value=\"Visible\"/></DataTrigger><DataTrigger Binding=\"{Binding Path=flag3}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"></Setter></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Button.Style></Button><Button x:Name=\"pages_button4\" Click=\"pages_buttons_Click\"><Button.Style><Style TargetType=\"{x:Type Button}\"><Setter Property=\"Width\" Value=\"30px\"/><Setter Property=\"Height\" Value=\"30px\"/><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Margin\" Value=\"10,0,0,0\"/><Setter Property=\"Content\" Value=\"{Binding button4}\"/><Setter Property=\"Visibility\" Value=\"Collapsed\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate><Border x:Name=\"boder\" BorderThickness=\"1,1,1,1\" BorderBrush=\"Black\" Background=\"White\"><TextBlock Text=\"{Binding button4}\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/></Border><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"BorderBrush\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/></Trigger><DataTrigger Binding=\"{Binding Path=page_flag4}\" Value=\"true\"><Setter Property=\"Visibility\" Value=\"Visible\"/></DataTrigger><DataTrigger Binding=\"{Binding Path=flag4}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"></Setter></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Button.Style></Button><Button x:Name=\"next_button\" Click=\"next_button_Click\"><Button.Style><Style TargetType=\"{x:Type Button}\"><Setter Property=\"Width\" Value=\"40px\"/><Setter Property=\"Height\" Value=\"30px\"/><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"Content\" Value=\">>\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Margin\" Value=\"10,0,0,0\"/><Setter Property=\"Visibility\" Value=\"Visible\"/><Setter Property=\"Tag\" Value=\"{Binding currentPage}\"></Setter><Setter Property=\"Template\"><Setter.Value><ControlTemplate><Border x:Name=\"boder\" BorderThickness=\"1,1,1,1\" BorderBrush=\"Black\" Background=\"White\"><TextBlock x:Name=\"text1\" Text=\">>\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/></Border><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"BorderBrush\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/></Trigger><DataTrigger Binding=\"{Binding Path=flag8}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/></DataTrigger><DataTrigger Binding=\"{Binding Path=flag10}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FFD7D7D7\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"/></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Button.Style></Button><Button x:Name=\"last_button\" Click=\"last_button_Click\"><Button.Style><Style TargetType=\"{x:Type Button}\"><Setter Property=\"Width\" Value=\"40px\"/><Setter Property=\"Height\" Value=\"30px\"/><Setter Property=\"HorizontalAlignment\" Value=\"Center\"/><Setter Property=\"Content\" Value=\"尾页\"/><Setter Property=\"VerticalAlignment\" Value=\"Center\"/><Setter Property=\"Margin\" Value=\"10,0,0,0\"/><Setter Property=\"Visibility\" Value=\"Visible\"/><Setter Property=\"Tag\" Value=\"{Binding pages}\"></Setter><Setter Property=\"Template\"><Setter.Value><ControlTemplate><Border x:Name=\"boder\" BorderThickness=\"1,1,1,1\" BorderBrush=\"Black\" Background=\"White\"><TextBlock x:Name=\"text1\" Text=\"尾页\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\"/></Border><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"BorderBrush\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/></Trigger><DataTrigger Binding=\"{Binding Path=flag10}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FFD7D7D7\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"/></DataTrigger><DataTrigger Binding=\"{Binding Path=flag6}\" Value=\"true\"><Setter Property=\"Background\" Value=\"#FF0DD1F0\" TargetName=\"boder\"/><Setter Property=\"BorderThickness\" Value=\"1\" TargetName=\"boder\"/><Setter Property=\"IsEnabled\" Value=\"False\"/></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Button.Style></Button><Label  Margin=\"30,0,0,0\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\"  Content=\"{Binding text}\"/></StackPanel><!--<StackPanel Grid.Column=\"1\" Orientation=\"Horizontal\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\"></StackPanel>--></Grid>

3,分页工具类,包括数据分页封装

[code]using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Model;//数据分页处理工具类namespace Util{public class PagesUntils{public static int count = 8;//每页显示数据5条//封装分页实体类public static Pages GetPages(List<BackUpRecord> backUpRecords, int currentPage,int flag) {//查询数据总数`int size = backUpRecords.Count();int pages;if (size > 0) {//计算总页数if (size % count == 0){pages = size / count;}else{pages = size / count + 1;}}else {pages = 1;}int button1 = 0;//定义按钮1的初始文本值int button2 = 0;//定义按钮2的初始文本值int button3 = 0;//定义按钮3的初始文本值int button4 = 0;//定义按钮4的初始文本值//初始化各种判断条件bool flag1 = false;bool flag2 = false;bool flag3 = false;bool flag4 = false;bool flag5 = false;bool flag6 = false;bool flag7 = false;bool flag8 = false;bool flag9 = false;bool flag10 = false;bool page_flag1 = false;bool page_flag2 = false;bool page_flag3 = false;bool page_flag4 = false;bool label_flag = false;//当前页等于总页数if (currentPage==pages) {flag10 = true;flag8 = true;}//首页 flag5if (-10 == currentPage){flag5 = true;flag7 = true;currentPage = 1;}//尾页  flag6if (-1 == currentPage){flag6 = true;flag10 = true;currentPage = pages;}//上一页 flag7if (-5 == currentPage){if (flag <= 0){flag7 = true;flag9 = true;flag5 = true;currentPage = 1;}else{flag7 = true;currentPage = flag;}}//下一页 flag8if (-6 == currentPage){if (flag >= pages){flag8 = true;flag4 = true;flag10 = true;currentPage = pages;}else{flag8 = true;currentPage = flag;}}if (currentPage <= 2 && pages >= 4){//当前显示第一页分页数据,并且总页数大于等于4。四个按钮都显示button1 = 1;button2 = 2;button3 = pages - 1;button4 = pages;}else if (currentPage > 2 && pages >= 4){button1 = pages - 3;button2 = pages - 2;button3 = pages - 1;button4 = pages;}else if (pages == 3){button1 = pages - 2;button2 = pages - 1;button3 = pages;button4 = 0;}else if (pages == 2){button1 = pages - 1;button2 = pages;button3 = 0;button4 = 0;}else if (pages == 1){button1 = pages;button2 = 0;button3 = 0;button4 = 0;}//右角下标拼接string str = \"共 \";str += pages;str += \" 页 ,\";str += size;str += \" 条数据\";//当前页对应哪个按钮的判断if (button1 == currentPage){flag1 = true;}if (button2 == currentPage){flag2 = true;}if (button3 == currentPage){flag3 = true;}if (button4 == currentPage){flag4 = true;}if (pages>=1) {page_flag1 = true;}if (pages>=2) {page_flag2 = true;}if (pages >= 3){page_flag3 = true;}if (pages >= 4){page_flag4 = true;}//是否显示省略号的判断if (pages > 4){label_flag = true;}Pages page = new Pages();page.count = count;page.pages = pages;page.sum = size;page.button1 = button1;page.button2 = button2;page.button3 = button3;page.button4 = button4;page.currentPage = currentPage;page.text = str;page.flag1 = flag1;page.flag2 = flag2;page.flag3 = flag3;page.flag4 = flag4;page.flag5 = flag5;page.flag6 = flag6;page.flag7 = flag7;page.flag8 = flag8;page.flag9 = flag9;page.flag10 = flag10;page.page_flag1 = page_flag1;page.page_flag2 = page_flag2;page.page_flag3 = page_flag3;page.page_flag4 = page_flag4;page.label_flag = label_flag;return page;}//显示每页数据public static List<T> data<T>(List<T> ts,int currentPage ) {List<T> dataTs = new List<T>();//计算当前页数据的开始索引int firstData = (currentPage - 1) * count;//计算当前页数据的结束索引int lastData = currentPage * count-1;for (int a=0;a<ts.Count;a++) {if (a>=firstData && a<=lastData) {dataTs.Add(ts[a]);}}return dataTs;}}}

4,cs

[code]    //分页初始化private void pg_Loaded(object sender, RoutedEventArgs e){pg.DataContext = PagesUntils.GetPages(FileUntils.readerData<BackUpRecord>(new BackUpRecord(), \"backUp/backUp.txt\"), 1,1);}//点击页码private void pages_buttons_Click(object sender, RoutedEventArgs e){Button btn = (Button)sender;int currentPage = (int)btn.Content;List<BackUpRecord> lists= FileUntils.readerData(new BackUpRecord(),fileName);pg.DataContext=PagesUntils.GetPages(lists, currentPage,1);dataGrid.ItemsSource = PagesUntils.data(lists, currentPage);}//点击首页或者尾页private void first_button_Click(object sender, RoutedEventArgs e){pg.DataContext = PagesUntils.GetPages(lists, -10,1);dataGrid.ItemsSource = PagesUntils.data(lists, 1);}//点击上一页private void up_button_Click(object sender, RoutedEventArgs e){Button btn = (Button)sender;var currentPage = btn.Tag;pg.DataContext = PagesUntils.GetPages(lists,-5, (int)currentPage - 1);dataGrid.ItemsSource = PagesUntils.data(lists, (int)currentPage-1<1 ? 1: (int)currentPage - 1);}//点击尾页private void last_button_Click(object sender, RoutedEventArgs e){Button btn = (Button)sender;var tag = btn.Tag;var pages= PagesUntils.GetPages(lists, -1, (int)tag);pg.DataContext = pages;dataGrid.ItemsSource = PagesUntils.data(lists, (int)tag);}//点击下一页private void next_button_Click(object sender, RoutedEventArgs e){Button btn = (Button)sender;var tag = btn.Tag;var pages = PagesUntils.GetPages(lists, -6, (int)tag+1);pg.DataContext = pages;//如果下一页大于总页数则显示最后一页,否则显示下一页dataGrid.ItemsSource = PagesUntils.data(lists, (int)tag+1>pages.pages?pages.pages:(int)tag+1);}}

四,再贴一下效果图

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C# WPF 分页插件