页面代码如下:

<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. 笔记本电脑没有Pause键,远程桌面无法全屏

    用过mstsc远程桌面的都知道,可以用CTRL+ALT+Break 切换为全屏操作,但有些品牌的电脑不知道设计理念是啥,居然没有Break键,解决办法就是用Fn+B键替换Break键,也就是同时按住C ...

  2. nodejs基础快速上手

    node 快速了解 hello node.js console.log("hello Node.js"); let http = require("http") ...

  3. @Override报错的处理

    有时候我们从SVN导的项目,jre和jar包都没问题,但是就会出如下图的错误: xi 点击红叉,显示错误信息如下: 点击工具里面的window-->preferences-->java-- ...

  4. CEF 自定义用户协议(scheme)实现以二进制流的方式显示图片、视频、音频

    转载:https://www.cnblogs.com/sinceret/p/10417941.html 转载:https://stackoverflow.com/questions/48811756/ ...

  5. TabLayout基本使用

    前言 Tablayout继承自HorizontalScrollView,可以用作顶部标签效果.底部导航栏效果.一般多与ViewPager一起使用. 想直接了解如何实现短下滑效果的请看:TabLayou ...

  6. Python3 tkinter基础 Spinbox 可输入 能调整的 从指定范围内选择参数的控件

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. elasticsearch_.net_client_nest2.x_到_5.x常用方法属性差异

    目录: Elasticsearch .net client NEST 5.x 使用总结 elasticsearch_.net_client_nest2.x_到_5.x常用方法属性差异 Elastics ...

  8. 最大子段和的DP算法设计及其效率测试

    表情包形象取自番剧<猫咪日常> 那我也整一个 曾几何时,笔者是个对算法这个概念漠不关心的人,由衷地感觉它就是一种和奥数一样华而不实的存在,即便不使用任何算法的思想我一样能写出能跑的程序 直 ...

  9. 【php】单例模式和工厂模式

    单例模式:防止重复实例化,避免大量的new操作,减少消耗系统和内存的资源,使得有且仅有一个实例对象 header("Content-type: text/html; charset=utf- ...

  10. mysql 通过测试'for update',深入了解行锁、表锁、索引

    mysql 通过测试'for update',深入了解行锁.表锁.索引 条件 FOR UPDATE 仅适用于InnoDB存储引擎,且必须在事务区块(BEGIN/COMMIT)中才能生效. mysql默 ...