页面代码如下:

<UserControl x:Class="Music163.DataGridPaging"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Music163"
mc:Ignorable="d"
d:DesignHeight="90" d:DesignWidth="800" Background="White">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Border x:Name="Border_Previous" Cursor="Hand" Width="36" Height="36" BorderBrush="#ddd" BorderThickness="1 1 1 1" CornerRadius="2" MouseLeftButtonDown="Border_Previous_MouseLeftButtonDown">
<Label Foreground="#666" FontSize="20px" HorizontalContentAlignment="Center" Content="<" VerticalContentAlignment="Center"></Label>
</Border>
<StackPanel x:Name="SP_NumberContainer" Orientation="Horizontal" VerticalAlignment="Center" >
</StackPanel>
<Border x:Name="Border_Next" Cursor="Hand" Width="36" Height="36" BorderBrush="#ddd" BorderThickness="1 1 1 1" Margin="8 0 20 0" CornerRadius="2" MouseLeftButtonDown="Border_Next_MouseLeftButtonDown">
<Label Foreground="#666" FontSize="20px" HorizontalContentAlignment="Center" Content=">" VerticalContentAlignment="Center"></Label>
</Border>
<Label Width="50" Height="36" VerticalContentAlignment="Center" Content="跳转到"></Label>
<TextBox x:Name="TB_CurrentIndex" Height="36" Width="36" HorizontalContentAlignment="Left"
InputMethod.IsInputMethodEnabled="False" TextChanged="TB_CurrentIndex_TextChanged"
VerticalContentAlignment="Center"
PreviewKeyDown="TB_CurrentIndex_PreviewKeyDown" PreviewTextInput="TB_CurrentIndex_PreviewTextInput">
</TextBox>
<Label Height="36" VerticalContentAlignment="Center" Padding="6" Content="页"></Label>
<ToggleButton Height="36" HorizontalAlignment="Center" Click="NavigateTo" Content="确定" Padding="4 0"></ToggleButton>
<Label Height="36" VerticalContentAlignment="Center" Margin="30 0 0 0" Content="每页20"></Label>
<Label Height="36" VerticalContentAlignment="Center" Content="/共"></Label>
<Label x:Name="Label_SumCount" Height="36" VerticalContentAlignment="Center" Content="0" Margin="4 0"></Label>
<Label Height="36" VerticalContentAlignment="Center" Content="条"></Label>
</StackPanel>
</UserControl>

 cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace Music163
{
public class GridPagingEventArgs : RoutedEventArgs
{
public GridPagingEventArgs(int size, int index)
{
PageSize = size;
PageIndex = index;
}
public int PageSize { get; set; }
public int PageIndex { get; set; }
} public delegate void GridPagingEventHandler(object sender, GridPagingEventArgs e); /// <summary>
/// DataGridPaging.xaml 的交互逻辑
/// </summary>
public partial class DataGridPaging : UserControl
{
public DataGridPaging()
{
InitializeComponent();
} public static readonly RoutedEvent GridPagingEvent = EventManager.RegisterRoutedEvent("GridPaging", RoutingStrategy.Bubble, typeof(GridPagingEventHandler), typeof(DataGridPaging));
public event GridPagingEventHandler GridPaging
{
add { this.AddHandler(GridPagingEvent, value); }
remove { this.RemoveHandler(GridPagingEvent, value); }
} /// <summary>
/// 创建"..."label
/// </summary>
/// <returns></returns>
private Label CreateDotLabel()
{
Label label = new Label();
label.Width = 36;
label.Height = 36;
label.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2aa1c8"));
label.HorizontalContentAlignment = HorizontalAlignment.Center;
label.VerticalContentAlignment = VerticalAlignment.Center;
label.Content = "...";
return label;
} private int _currentIndex = 1;
/// <summary>
/// 记录当前所选页码
/// </summary>
public int CurrentIndex
{
get { return _currentIndex; }
} private int _currentSize = 20;
/// <summary>
/// 当前一页长度
/// </summary>
public int CurrentSize
{
get { return _currentSize; }
} private int _currentCount = 0;
/// <summary>
/// 当前总条数
/// </summary>
public int CurrentCount
{
get { return _currentCount; }
} /// <summary>
/// 当前总页数
/// </summary>
private int _currentPageCount = 0; /// <summary>
/// 非选中的数字
/// </summary>
/// <param name="number"></param>
/// <param name="borderThickness"></param>
/// <returns></returns>
private Border CreateUnSelectNumberBorder(int number, Thickness borderThickness)
{
Border border = new Border();
border.MinWidth = 36;
border.Cursor = Cursors.Hand;
border.Height = 36;
border.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ddd"));
border.BorderThickness = borderThickness;
border.CornerRadius = new CornerRadius(2);
border.Margin = new Thickness(8, 0, 0, 0);
border.Child = new Label()
{
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#666")),
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Content = number.ToString()
};
return border;
} /// <summary>
/// 当前选中的数字
/// </summary>
/// <param name="number"></param>
/// <param name="borderThickness"></param>
/// <returns></returns>
private Border CreateSelectNumberBorder(int number, Thickness borderThickness)
{
Border border = new Border();
border.MinWidth = 36;
border.Height = 36;
border.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ddd"));
border.BorderThickness = borderThickness;
border.CornerRadius = new CornerRadius(2);
border.Margin = new Thickness(8, 0, 0, 0);
border.Child = new Label()
{
Foreground = new SolidColorBrush(Colors.White),
Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2aa1c8")),
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Content = number.ToString()
};
return border;
}
/// <summary>
/// 列表查询以后调用该方法重置分页控件
/// </summary>
/// <param name="size">每页条数</param>
/// <param name="index">当前第几页</param>
/// <param name="sumcount">总共条数</param>
public void ResetPage(int size, int index, int sumcount)
{
_currentIndex = index;
_currentSize = size;
_currentCount = sumcount;
SP_NumberContainer.Children.Clear();
_currentPageCount = sumcount / size;
if (sumcount % size > 0)
{
_currentPageCount++;
}
if (_currentPageCount <= 8)//直接显示所有页号
{
for (int i = 1; i <= _currentPageCount; i++)
{
if (i != index)//非选中
{
Border unSelectNumber = CreateUnSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
unSelectNumber.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(unSelectNumber);
}
else//选中
{
Border b = CreateSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
SP_NumberContainer.Children.Add(b);
}
}
}
else//两边增加...
{
if (index <= 4)//当前选中的是头四页
{
for (int i = 1; i <= 5; i++)
{
if (i == index)
{
Border b = CreateSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
SP_NumberContainer.Children.Add(b);
}
else
{
Border unSelectNumber = CreateUnSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
unSelectNumber.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(unSelectNumber);
}
}
SP_NumberContainer.Children.Add(CreateDotLabel()); Border lastNumber = CreateUnSelectNumberBorder(_currentPageCount, new Thickness(1, 1, 1, 1));
lastNumber.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(lastNumber);
}
else if (index > 4 && index <= _currentPageCount - 4)//中间
{
Border firstNumber = CreateUnSelectNumberBorder(1, new Thickness(1, 1, 1, 1));
firstNumber.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(firstNumber);
SP_NumberContainer.Children.Add(CreateDotLabel()); for (int i = index - 2; i <= index + 2; i++)
{
if (i == index - 2)
{
Border b = CreateUnSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
b.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(b);
continue;
}
else
{
if (i == index)
{
Border b = CreateSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
SP_NumberContainer.Children.Add(b);
}
else
{
Border b = CreateUnSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
b.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(b);
}
}
} SP_NumberContainer.Children.Add(CreateDotLabel());
Border lastNumber = CreateUnSelectNumberBorder(_currentPageCount, new Thickness(1, 1, 1, 1));
lastNumber.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(lastNumber);
}
else if (index > _currentPageCount - 4)//尾四页
{
Border firstNumber = CreateUnSelectNumberBorder(1, new Thickness(1, 1, 1, 1));
firstNumber.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(firstNumber);
SP_NumberContainer.Children.Add(CreateDotLabel());
for (int i = _currentPageCount - 4; i <= _currentPageCount; i++)
{
if (i == _currentPageCount - 4)
{
Border b = CreateUnSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
b.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(b);
continue;
}
if (i == index)
{
Border b = CreateSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
SP_NumberContainer.Children.Add(b);
}
else
{
Border b = CreateUnSelectNumberBorder(i, new Thickness(1, 1, 1, 1));
b.MouseLeftButtonDown += UnSelectNumber_MouseLeftButtonDown;
SP_NumberContainer.Children.Add(b);
}
}
}
}
Label_SumCount.Content = sumcount.ToString();
TB_CurrentIndex.Text = index.ToString();
} /// <summary>
/// 换页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UnSelectNumber_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Border b = sender as Border;
GridPagingEventArgs args = new GridPagingEventArgs(20, Convert.ToInt32((b.Child as Label).Content));
args.RoutedEvent = GridPagingEvent;
RaiseEvent(args);
} /// <summary>
/// 前一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Border_Previous_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_currentIndex == 1) return;
GridPagingEventArgs args = new GridPagingEventArgs(20, _currentIndex - 1);
args.RoutedEvent = GridPagingEvent;
RaiseEvent(args);
} /// <summary>
/// 后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Border_Next_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_currentIndex == _currentPageCount) return;
GridPagingEventArgs args = new GridPagingEventArgs(20, _currentIndex + 1);
args.RoutedEvent = GridPagingEvent;
RaiseEvent(args);
} /// <summary>
/// 更换每页条数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CB_PageSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_currentIndex = 1;
GridPagingEventArgs args = new GridPagingEventArgs(20, _currentIndex);
args.RoutedEvent = GridPagingEvent;
RaiseEvent(args);
} /// <summary>
/// 检测粘贴
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TB_CurrentIndex_Pasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(String)))
{
String text = (String)e.DataObject.GetData(typeof(String));
if (!isNumberic(text))
{ e.CancelCommand(); }
}
else { e.CancelCommand(); }
} /// <summary>
/// 是否数字
/// </summary>
/// <param name="_string"></param>
/// <returns></returns>
public static bool isNumberic(string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
foreach (char c in _string)
{
if (!char.IsDigit(c))
return false;
}
return true;
} private void TB_CurrentIndex_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
e.Handled = true; } private void TB_CurrentIndex_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!isNumberic(e.Text))
{
e.Handled = true;
}
else
{
e.Handled = false; }
} private void TB_CurrentIndex_TextChanged(object sender, TextChangedEventArgs e)
{
TB_CurrentIndex.Width = 36.0 + TB_CurrentIndex.Text.Length * 8;
//int index;
//if (int.TryParse(TB_CurrentIndex.Text, out index))
//{
// if (index != _currentIndex)
// {
// if (index <= _currentPageCount)
// {
// _currentIndex = index;
// }
// else
// {
// _currentIndex = _currentPageCount;
// }
// GridPagingEventArgs args = new GridPagingEventArgs(Convert.ToInt32((CB_PageSize.SelectedItem as ComboBoxItem).Content), _currentIndex);
// args.RoutedEvent = GridPagingEvent;
// RaiseEvent(args);
// }
//}
} private void ToggleButton_Click(object sender, RoutedEventArgs e)
{ } private void NavigateTo(object sender, RoutedEventArgs e)
{
int index;
if (int.TryParse(TB_CurrentIndex.Text, out index))
{
if (index != _currentIndex)
{
if (index <= _currentPageCount)
{
_currentIndex = index;
}
else
{
_currentIndex = _currentPageCount;
}
GridPagingEventArgs args = new GridPagingEventArgs(20, _currentIndex);
args.RoutedEvent = GridPagingEvent;
RaiseEvent(args);
}
}
}
}
}

 调用的地方实现GridPaging方法,初始化的时候调用ResetPage方法即可:

       <local:DataGridPaging x:Name="DGP_Main" Grid.Row="3"  GridPaging="DataGridPaging_GridPaging"/>
  DGP_Main.ResetPage(20, offset + 1, sumcount);

  

WPF实现分页控件的更多相关文章

  1. WPF自定义分页控件,样式自定义,简单易用

    WPF自定义分页控件 做了许久伸手党,终于有机会贡献一波,搜索一下WPF分页控件,还是多,但是不太通用,主要就是样式问题,这个WPF很好解决,还有一个就是分页控件嘛,只关心几个数字的变动就行了,把页码 ...

  2. WPF 自定义分页控件二

    一:添加自定义分页控件,命名为KDataPagerTwo: public class KDataPagerTwo : Control, INotifyPropertyChanged { static ...

  3. WPF 自定义分页控件一

    一:右键添加新建项,选择新建自定义控件,命名为:KDataPager public class KDataPager : Control { static KDataPager() { Default ...

  4. WPF 实现 DataGrid/ListView 分页控件

    在WPF中,通常会选用DataGrid/ListView进行数据展示,如果数据量不多,可以直接一个页面显示出来.如果数据量很大,2000条数据,一次性显示在一个页面中,不仅消耗资源,而且用户体验也很糟 ...

  5. WPF MVVM 用户控件完成分页

    项目中经常会有分页查询的情况,在WPF中我们可以通过用户控件完成分页 一下为分页控件的页面代码, <UserControl x:Class="Foundation.UCtrl.Next ...

  6. WPF 分页控件 WPF 多线程 BackgroundWorker

    WPF 分页控件 WPF 多线程 BackgroundWorker 大家好,好久没有发表一篇像样的博客了,最近的开发实在头疼,很多东西无从下口,需求没完没了,更要命的是公司的开发从来不走正规流程啊, ...

  7. 两款不同应用场景的Wpf分页控件

    简介 今天给大家分享两个Wpf分页控件,本篇博客主要介绍一些实现思路和使用方法,具体实现和应用代码请参考文末的Demo链接 废话不多说,先看一下效果~ (两款控件显示效果是一样的) 实现思路 一款控件 ...

  8. WPF自定义DataGrid分页控件

    新建Custom Control,名:PagingDataGrid 打开工程下面的Themes\Generic.xaml xaml里面代码替换如下 <Style x:Key="{x:T ...

  9. WPF管理系统自定义分页控件 - WPF特工队内部资料

    最近做一个演示的管理系统项目,需要用到分页控件,在网上找了很多,依然找到与UI模版匹配的,最后干脆自己写一个. 分页控件分析: 1.分页控件分简单显示和复杂显示两种: 2.包含上一页.下一页以及页码明 ...

随机推荐

  1. GO语言从入门到放弃目录

    GO语言基础 第一个GO程序 GO语言常量和变量 GO语言数据类型 GO语言流程控制 GO语言数组 GO语言切片 GO语言 map GO语言函数 GO语言指针 Go语言接口 GO语言常用包 GO语言的 ...

  2. MongoDB 3.4 安装以 Windows 服务方式运行

    1.首先从https://www.mongodb.com/download-center#community 下载社区版,企业版也是类似. 2.双击运行安装,可自定义安装路径,这里采用默认路径(C:\ ...

  3. Qt文档阅读笔记-QGraphicsItem::paint中QStyleOptionGraphicsItem *option的进一步认识

    官方解析 painter : 此参数用于绘图;option : 提供了item的风格,比如item的状态,曝光度以及详细的信息:widget : 想画到哪个widget上,如果要画在缓存区上,这个参数 ...

  4. GCOV&LCOV&GCOVR入门

    索引 一.概述 二.关于gcov的安装 三.代码覆盖率测试(以GCOV为例) 1.编译源代码 2.运行可执行程序 3.通过gcov指令生成代码覆盖率报告 四.生成更全面.直观的代码覆盖率报告 1.LC ...

  5. 【题解】Luogu P3674 小清新人渣的本愿

    原题传送门 这题还算简单(我记得我刚学oi时就来写这题,然后暴力都爆零了) 看见无修改,那么这题应该是莫队 维护两个bitset,第二个是第一个的反串,bitset内维护每个数字是否出现过 第一种操作 ...

  6. c++的虚继承

    今天去面试了一家公司,真是套路深啊,套路深,原谅我是后知后觉,所以人吧总的长大,出差正常情况下都是有补贴的,为啥这部分也要算我工资一部分,名其名曰工资高,哈哈哈,自古套路方得人心 今天学习了一下c++ ...

  7. JS getBoundingClientRect()

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Web 端异步下载文件

    Web 端异步下载文件 实现文件异步下载: 在服务端无法返回文件,或发生异常时给予提示. JavaScript: 服务端返回的JSON对象形如: { code:200, msg:'下载成功|未找到指定 ...

  9. 北京AI外包团队 祝大家2019事业有事,大吉大利!

    未来已来,以人工智能为核心的科技力量,在重新塑造着我们生活的环境.这种重新塑造的现象如此之明显,力量如此强大,以至于越来越多的人在讨论,我们面临着新一轮的工业革命.而且现在我们面临的这次新的科技力量, ...

  10. 安装卡巴 OFFICE链接 出现这个过程被中断,由于本机的限制

    今天 安装了卡巴后 office 超链接功能不能使用了,一点击超链接,就会发出警报,说”由于本机的限制,此操作已被取消,请与系统管理员联系“ 解决办法:1打开注册表2到这个位置:HKEY_CURREN ...