WPF实现分页控件

时间:2021-02-22 00:36:23

页面代码如下:

<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自定义分页控件&comma;样式自定义&comma;简单易用

    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&sol;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. AutoTransformHandler

    public static ObservableCollection<F> Transform<T, F>(List<T> target) where F : ne ...

  2. Enum Types

    参考Java的官方tutorial和Doc整理如下. What is Enum An enum type is a special data type. It enables for a variab ...

  3. 白话图解HTTPS原理

        [前言]最近看过几篇文章,内容是关于"全民HTTPS"的.为什么HTTPS,突然会受到如此多业内人士的青睐呢?HTTPS究竟是什么呢?它与HTTP又有怎样的区别呢? 带着这 ...

  4. Linux下DIR,dirent&comma;stat等结构体详解

    摘自:http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/ 最近在看Linux下文件操作相关章节,遇到了这 ...

  5. Kafka 0&period;8 Controller设计机制和状态变化

    在kafka集群中,其中一个broker server作为*控制器Control,负责管理分区和副本状态并执行管理着这些分区的重新分配. 下面说明如何通过*控制器操作分区和副本的状态. 名词解释 ...

  6. FastAdmin Bootstrap-table 特定某行背景变红

    FastAdmin Bootstrap-table 特定某行背景变红 rowStyle: function (row, index) { var style = {css:{'background': ...

  7. 加载log文件

    procedure Jslog(szLogText:string);var pMyFile : TextFile;begin Assignfile(pMyFile, 'log.txt'); if Fi ...

  8. PJzhang&colon;漏洞渗透测试框架&OpenCurlyDoubleQuote;天使之剑(AngelSword)”

    猫宁!!! 参考链接: www.phpinfo.cc/?post=42 https://www.freebuf.com/sectool/149883.html 同事介绍了一款渗透测试框架AngelSw ...

  9. vbnet 进程监控,监控Acad开启的数量,并且添加到开机启动

    1# 自定义函数,添加到注册表 Public Sub StartRunRegHKLM() REM HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Micro ...

  10. HDU-1257 最少拦截系统 贪心&sol;DP 最长上升子序列的长度&equals;&equals;最长不上升子序列的个数?

    题目链接:https://cn.vjudge.net/problem/HDU-1257 题意 中文题咯中文题咯 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然 ...