本文为伪原创原文为 NET未来之路的https://www.cnblogs.com/lonelyxmas/p/10641255.html

原文中,页码数量会不断增加,会将下一页的按钮顶出去

修改了一下本人认为不合理的地方,以供学习。

效果:

下面代码

前台:

<UserControl x:Class="SwitchModule.Controls.Pager"
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:SwitchModule.Controls"
mc:Ignorable="d"
>
<UserControl.Resources> <PathGeometry x:Key="prev" Figures="M921.6 1024 102.4 1024C45.824 1024 0 978.176 0 921.6L0 102.4C0 45.824 45.824 0 102.4 0L921.6 0C978.176 0 1024 45.824 1024 102.4L1024 921.6C1024 978.176 978.176 1024 921.6 1024ZM640.1536 226.1504C633.088 226.1504 626.7904 229.2224 622.3872 234.0864L361.3184 495.1552 361.3184 495.1552C357.0176 499.456 354.304 505.4464 354.304 512.1024 354.304 518.6048 356.9152 524.544 361.1648 528.8448L361.1648 528.896 622.848 790.5792C623.0016 790.7328 623.104 790.8352 623.2576 790.9888L623.2576 790.9888C627.6096 795.1872 634.2144 794.8288 639.9488 797.7984 673.9968 815.36 687.872 787.0976 687.872 773.8368 687.872 767.3856 685.312 761.4976 681.1136 757.1968L681.1136 757.1968 680.96 757.0432C680.9088 756.992 680.8576 756.9408 680.8064 756.8896L436.0192 512.1024 681.1136 267.008 681.1136 267.008C685.4144 262.656 691.968 255.4368 688.0768 250.0608 654.4896 203.264 653.3632 226.1504 640.1536 226.1504Z"></PathGeometry> <Style x:Key="PrevPage" TargetType="{x:Type Button}">
<!--<Setter Property="Width" Value="30"></Setter>-->
<Setter Property="Height" Value="28"></Setter>
<Setter Property="Foreground" Value="#22CB64"></Setter>
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="PART_root" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<Viewbox Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Path Data="{StaticResource prev}" Fill="{Binding Path=Foreground,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource TemplatedParent}}"></Path>
</Viewbox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="#FFB800"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background" Value="#22CB64"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Gray"></Setter>
<Setter Property="Background" Value="Black"></Setter>
</Trigger>
</Style.Triggers>
</Style> <Style x:Key="NextPage" TargetType="{x:Type Button}">
<!--<Setter Property="Width" Value="30"></Setter>-->
<Setter Property="Height" Value="28"></Setter>
<Setter Property="Foreground" Value="#22CB64"></Setter>
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="PART_root" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<Viewbox Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Path Data="{StaticResource prev}" RenderTransformOrigin="0.5,0.5" Fill="{Binding Path=Foreground,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource TemplatedParent}}">
<Path.RenderTransform>
<RotateTransform Angle="180"></RotateTransform>
</Path.RenderTransform>
</Path>
</Viewbox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="#FFB800"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background" Value="#22CB64"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Gray"></Setter>
<Setter Property="Background" Value="Black"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<!--上一页-->
<Button x:Name="btnPrePage" Style="{StaticResource PrevPage}" Click="btnPrePage_Click" CommandParameter="{Binding}"
VerticalAlignment="Center" Visibility="{Binding NumVisible}"> </Button>
<!--页码-->
<ItemsControl x:Name="itemsControl">
<ItemsControl.Template>
<ControlTemplate>
<ItemsPresenter></ItemsPresenter>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Button x:Name="btnNum" Height="28" Background="Transparent" Click="btnNum_Click"
CommandParameter="{Binding}" VerticalAlignment="Center" Visibility="{Binding NumVisible}">
<Button.Template>
<ControlTemplate>
<Border x:Name="border" Background="Transparent" CornerRadius="2" SnapsToDevicePixels="True">
<TextBlock x:Name="txt" Margin="10 0 10 0" Foreground="{Binding CurrentPageColor}"
FontSize="{Binding FontSize}" FontWeight="Bold" Text="{Binding Page}" VerticalAlignment="Center" ></TextBlock>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="border" Property="Background" Value="#6633ccee"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Border Height="28" Visibility="{Binding OmitVisible}">
<TextBlock x:Name="txt" Margin="10 0 10 3" Foreground="Black" FontSize="{Binding FontSize}" Text="…" VerticalAlignment="Center" ></TextBlock>
</Border>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!--下一页-->
<Button x:Name="btnNextPage" Style="{StaticResource NextPage}" Click="btnNextPage_Click" CommandParameter="{Binding}" VerticalAlignment="Center" Visibility="{Binding NumVisible}"> </Button>
</StackPanel>
</Grid>
</UserControl>

后台逻辑

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media; namespace SwitchModule.Controls
{
/// <summary>
/// 分页控件
/// </summary>
public partial class Pager : UserControl, INotifyPropertyChanged
{
#region 事件
/// <summary>
/// 分页事件
/// </summary>
public event EventHandler<PageChangedEventArgs> PageChanged;
#endregion #region 变量
private ObservableCollection<PageControlItemModel> _collection = new ObservableCollection<PageControlItemModel>();
private List<PageControlItemModel> _list = null;
#endregion #region 属性
private int _FontSize = ;
/// <summary>
/// 文字字体大小
/// </summary>
public new int FontSize
{
get { return _FontSize; }
set
{
_FontSize = value;
OnPropertyChanged("FontSize"); CalcPageNumList(); //计算页码
}
}
#endregion #region 分页相关属性
///// <summary>
///// 总页数
///// </summary>
public static readonly DependencyProperty PageCountProperty = DependencyProperty.Register(
"PageCount", typeof(int), typeof(Pager), new PropertyMetadata()); public int PageCount
{
get { return (int)GetValue(PageCountProperty); }
set { SetValue(PageCountProperty, value); }
} ///// <summary>
///// 当前页码
///// </summary>
public static readonly DependencyProperty PageProperty = DependencyProperty.Register(
"Page", typeof(int), typeof(Pager), new PropertyMetadata()); public int Page
{
get { return (int)GetValue(PageProperty); }
set { SetValue(PageProperty, value); }
} ///// <summary>
///// 记录总数
///// </summary>
public static readonly DependencyProperty RecordCountProperty = DependencyProperty.Register(
"RecordCount", typeof(int), typeof(Pager), new PropertyMetadata()); public int RecordCount
{
get { return (int)GetValue(RecordCountProperty); }
set { SetValue(RecordCountProperty, value); }
} ///// <summary>
///// 每页记录数
///// </summary>
public static readonly DependencyProperty PageSizeProperty = DependencyProperty.Register(
"PageSize", typeof(int), typeof(Pager), new PropertyMetadata()); public int PageSize
{
get { return (int)GetValue(PageSizeProperty); }
set { SetValue(PageSizeProperty, value); }
} private int _ContinuousCount = ;
/// <summary>
/// 当前页码右边连续页码数
/// </summary>
public int ContinuousCount
{
get { return _ContinuousCount; }
set
{
_ContinuousCount = value;
OnPropertyChanged("_ContinuousCount"); CalcPageNumList(); //计算页码
}
}
#endregion #region 构造函数
public Pager()
{
InitializeComponent(); this.itemsControl.ItemsSource = _collection;
}
#endregion #region 单击页码事件
private void btnNum_Click(object sender, RoutedEventArgs e)
{
if (PageChanged != null)
{
Button btn = sender as Button;
PageControlItemModel itemModel = btn.CommandParameter as PageControlItemModel;
if (itemModel.Page != Page)
{
Page = itemModel.Page;
CalcPageNumList(); PageChangedEventArgs args = new PageChangedEventArgs(itemModel.Page);
PageChanged(sender, args);
}
}
}
#endregion #region 计算页码
/// <summary>
/// 计算页码
/// </summary>
private void CalcPageNumList()
{
PageCount = (RecordCount - ) / PageSize + ; //计算总页数PageCount _list = new List<PageControlItemModel>(); //第一页
PageControlItemModel item = new PageControlItemModel(, Page);
_list.Add(item); int itemCnt = ; int _page = ; itemCnt = ContinuousCount + Page > PageCount ? PageCount : ContinuousCount + Page;
if (ContinuousCount + Page > PageCount)
{
_page = PageCount - ContinuousCount>0? PageCount - ContinuousCount:1;
}
else
{
_page = Page;
}
for (int i = _page; i < itemCnt; i++)
{ item = new PageControlItemModel(i, Page);
if (!_list.Exists(a => a.Page == item.Page))
{
_list.Add(item);
}
} //最后一页
item = new PageControlItemModel(PageCount, Page);
if (!_list.Exists(a => a.Page == item.Page))
{
_list.Add(item);
} for (int i = _list.Count - ; i > ; i--)
{
if (_list[i].Page - _list[i - ].Page > )
{
_list.Insert(i, new PageControlItemModel(, Page, ));
}
} //上一页下一页
if (Page == )
{
this.btnPrePage.IsEnabled = false; }
else
{
this.btnPrePage.IsEnabled = true; }
if (Page == PageCount)
{
this.btnNextPage.IsEnabled = false; }
else
{
this.btnNextPage.IsEnabled = true; } _collection.Clear();
_list.ForEach(a => { _collection.Add(a); });
}
#endregion #region 上一页
private void btnPrePage_Click(object sender, RoutedEventArgs e)
{
int prePage = Page - ;
if (prePage < ) prePage = ;
if (prePage != Page)
{
Page = prePage;
CalcPageNumList(); PageChangedEventArgs args = new PageChangedEventArgs(prePage);
PageChanged(sender, args);
}
}
#endregion #region 下一页
private void btnNextPage_Click(object sender, RoutedEventArgs e)
{
int nextPage = Page + ;
if (nextPage > PageCount) nextPage = PageCount;
if (nextPage != Page)
{ Page = nextPage;
CalcPageNumList(); PageChangedEventArgs args = new PageChangedEventArgs(nextPage);
PageChanged(sender, args);
}
}
#endregion #region INotifyPropertyChanged接口
public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
} protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == PageSizeProperty)
{
CalcPageNumList();
}
else if (e.Property == RecordCountProperty)
{
CalcPageNumList();
}
} #endregion } #region 分页控件Item Model
/// <summary>
/// 分页控件Item Model
/// </summary>
public class PageControlItemModel : INotifyPropertyChanged
{
private int _Type = ;
/// <summary>
/// 类型(1数字 2省略号)
/// </summary>
public int Type
{
get { return _Type; }
set
{
_Type = value;
OnPropertyChanged("Type"); if (_Type == )
{
NumVisible = Visibility.Visible;
OmitVisible = Visibility.Collapsed;
}
else
{
NumVisible = Visibility.Collapsed;
OmitVisible = Visibility.Visible;
}
}
} private bool _IsCurrentPage;
/// <summary>
/// 是否当前页码
/// </summary>
public bool IsCurrentPage
{
get { return _IsCurrentPage; }
set
{
_IsCurrentPage = value;
OnPropertyChanged("IsCurrentPage"); if (_IsCurrentPage)
{
CurrentPageColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#22CB64"));
}
else
{
CurrentPageColor = new SolidColorBrush(Colors.Black);
}
}
} private SolidColorBrush _CurrentPageColor = new SolidColorBrush(Colors.Green);
/// <summary>
/// 当前页码颜色
/// </summary>
public SolidColorBrush CurrentPageColor
{
get { return _CurrentPageColor; }
set
{
_CurrentPageColor = value;
OnPropertyChanged("CurrentPageColor");
}
} private int _Page;
/// <summary>
/// 页码
/// </summary>
public int Page
{
get { return _Page; }
set
{
_Page = value;
OnPropertyChanged("Page");
}
} private Visibility _NumVisible = Visibility.Visible;
/// <summary>
/// 数字可见
/// </summary>
public Visibility NumVisible
{
get { return _NumVisible; }
set
{
_NumVisible = value;
OnPropertyChanged("NumVisible");
}
} private Visibility _OmitVisible = Visibility.Collapsed;
/// <summary>
/// 省略号可见
/// </summary>
public Visibility OmitVisible
{
get { return _OmitVisible; }
set
{
_OmitVisible = value;
OnPropertyChanged("OmitVisible");
}
} /// <summary>
/// 分页控件Item Model
/// </summary>
/// <param name="page">页码</param>
/// <param name="currentPage">当前页码</param>
/// <param name="type">类型(1数字 2省略号)</param>
public PageControlItemModel(int page, int currentPage, int type = )
{
Type = type;
Page = page;
IsCurrentPage = page == currentPage;
} #region INotifyPropertyChanged接口
public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion }
#endregion #region 分页事件参数
/// <summary>
/// 分页事件参数
/// </summary>
public class PageChangedEventArgs : EventArgs
{
private int _Page = ;
/// <summary>
/// 当前页码
/// </summary>
public int Page
{
get
{
return _Page;
}
} /// <summary>
/// 分页事件参数
/// </summary>
/// <param name="page">当前页码</param>
public PageChangedEventArgs(int page)
{
_Page = page;
}
}
#endregion }

最新文章

  1. gitlab基本维护和使用
  2. NSCache和NSURLCache、网络缓存优化
  3. 尝试使用Memcached遇到的狗血问题
  4. ubuntu16.04装MatConvNet
  5. POI给Excel添加数字筛选
  6. HBase的RowKey设计原则
  7. [数据库操作]Java中的JDBC的使用方法.
  8. bzoj2764 基因补全
  9. linux的串口驱动分析
  10. vs2010中的反编译
  11. 【原创】lua编译时发现缺少readline库
  12. PHP程序缓存之文件缓存处理方式
  13. Qt之自定义托盘(两种方法)
  14. HDU 4661 Message Passing 【Tree】
  15. beanutils中WrapDynaBean
  16. js的变量声明以及变量提升
  17. 编写一个python脚本功能-备份
  18. C#学习-子类的初始化顺序
  19. POJ1743 Musical Theme(后缀数组 二分)
  20. SMB协议原理抓包分析

热门文章

  1. celery 动态定时任务探索
  2. (一)流--IO框架
  3. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二章:矩阵代数
  4. SDUT-2107_图的深度遍历
  5. Java练习 SDUT-3081_谁是最强女汉子
  6. MUI - 为textarea添加语音输入和清除的功能
  7. day15 web前端之css
  8. js获取当前日期及获取当前日期的前一天日期函数
  9. CNN网络改善的方法——池化
  10. Redis源码解析:03字典