页面代码如下:

<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. 移动端js调试工具:eruda

    通常写前端页面都在Chrome浏览器的开发模式下进行调试,但是写放在移动端的H5页面时,有时候会遇到在Chrome上调试没有问题,但是在手机的浏览器上有问题的情况:或者有些功能只能在特定的容器中才能其 ...

  2. js判断终端以及APP应用判断

    **第一种:通过判断浏览器的userAgent,用正则来判断是否是ios和Android客户端.代码如下:** <script type="text/javascript"& ...

  3. mysql的sql_mode设置

    参考官方文档: mysql可以为不同的客户端设置不同的sql_mode,并且每个应用能够设置他自己的会话级别的sql_mode.sql_mode会影响sql语法以及mysql显示数据的正确性. Whe ...

  4. Ubuntu 无界面使用selenium chrome + headless

    1. 安装 selenium : sudo pip install selenium 2. 安装 chromdriver: 进入 淘宝镜像源 下载 chromdriver, 可以查看 notes.tx ...

  5. 不校验csrf

    from django.views.decorators.csrf import csrf_exempt@csrf_exemptdef a(request): pass

  6. jQuary学习の五のAJAX

    AJAX 是与服务器交换数据的技术,它在不重载全部页面的情况下,实现了对部分网页的更新. 一.jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. loa ...

  7. C# 简单粗暴写日志

    public static void WriteLog(string text) { string path = AppDomain.CurrentDomain.BaseDirectory; path ...

  8. Unity 2018.2.8 旧版本安装包和破解软件

    声明:本文所提供的所有软件均来自于互联网,仅供个人研究和学习使用,请勿用于商业用途,下载后请于24小时内删除,请支持正版! 最近Unity官网下载的旧版本,都无法正常破解.此链接有之前下载的离线安装包 ...

  9. 【转载】RESTful 架构风格概述

    本文转载自https://blog.igevin.info/posts/restful-architecture-in-general/ 在移动互联网的大潮下,随着docker等技术的兴起,『微服务』 ...

  10. laravel——基础增删改查

    一.控制器代码 <?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class CurdContro ...