WPF自定义分页控件

做了许久伸手党,终于有机会贡献一波,搜索一下WPF分页控件,还是多,但是不太通用,主要就是样式问题,这个WPF很好解决,还有一个就是分页控件嘛,只关心几个数字的变动就行了,把页码改变事件暴露出来,数据的加载在这里就做就行,所以这个分页控件很简单...

好像也没啥讲的,直接上代码了

分页控件基本样式

<Style TargetType="{x:Type local:Pager}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Pager}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment"
Value="Center"></Setter>
</Style>
</StackPanel.Resources>
<TextBlock Text="共"></TextBlock>
<TextBlock x:Name="PART_TotalCount">
</TextBlock>
<TextBlock Text="行,"></TextBlock>
<TextBlock Text="每页"></TextBlock>
<TextBlock x:Name="PART_PageSize">
</TextBlock>
<TextBlock Text="行,"></TextBlock>
<TextBlock Text="第">
</TextBlock>
<TextBlock x:Name="PART_PageIndex"></TextBlock>
<TextBlock Text="/"></TextBlock>
<TextBlock x:Name="PART_TotalPage"></TextBlock>
<TextBlock Text="页"></TextBlock>
<Button Margin="10,0,0,0"
Content="首页"
x:Name="PART_FirstPage"></Button>
<Button Margin="10,0,0,0"
Content="上一页"
x:Name="PART_PrePage"></Button>
<Button Margin="10,0,0,0"
Content="下一页"
x:Name="PART_NextPage"></Button>
<Button Margin="10,0,10,0"
Content="尾页"
x:Name="PART_LastPage"></Button>
<TextBox Width="50"
x:Name="PART_PageNum"></TextBox>
<Button Margin="10,0,0,0"
Content="转到"
x:Name="PART_PageGo"></Button>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

分页控件代码

[TemplatePart(Name = "PART_TotalCount", Type = typeof(TextBlock))]
[TemplatePart(Name = "PART_PageSize", Type = typeof(TextBlock))]
[TemplatePart(Name = "PART_PageIndex", Type = typeof(TextBlock))]
[TemplatePart(Name = "PART_TotalPage", Type = typeof(TextBlock))]
[TemplatePart(Name = "PART_FirstPage", Type = typeof(Button))]
[TemplatePart(Name = "PART_PrePage", Type = typeof(Button))]
[TemplatePart(Name = "PART_NextPage", Type = typeof(Button))]
[TemplatePart(Name = "PART_LastPage", Type = typeof(Button))]
[TemplatePart(Name = "PART_PageGo", Type = typeof(Button))]
[TemplatePart(Name = "PART_PageNum", Type = typeof(TextBox))]
public class Pager : Control
{
#region 字段 private TextBlock _txtTotalCount;
private TextBlock _txtPageSize;
private TextBlock _txtPageIndex;
private TextBlock _txtTotalPage;
private Button _btnFirstPage;
private Button _btnPrePage;
private Button _btnNextPage;
private Button _btnLastPage;
private Button _btnPageGo;
private TextBox _txtBoxPageNum; #endregion #region 静态构造
static Pager()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Pager), new FrameworkPropertyMetadata(typeof(Pager)));
}
#endregion #region 依赖属性 #region 总记录数 public int TotalCount
{
get
{
return (int)GetValue(TotalCountProperty);
}
set
{
SetValue(TotalCountProperty, value);
}
} // Using a DependencyProperty as the backing store for TotalCount. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TotalCountProperty =
DependencyProperty.Register("TotalCount", typeof(int), typeof(Pager), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(obj, e) =>
{
var pager = obj as Pager; if (pager == null) return; pager.SetTotalCount((int)e.NewValue);
})); #endregion #region 每页大小 public int PageSize
{
get
{
return (int)GetValue(PageSizeProperty);
}
set
{
SetValue(PageSizeProperty, value);
}
} // Using a DependencyProperty as the backing store for PageSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageSizeProperty =
DependencyProperty.Register("PageSize", typeof(int), typeof(Pager), new FrameworkPropertyMetadata(0,
(obj, e) =>
{
var pager = obj as Pager; if (pager == null) return; pager.SetPageSize((int)e.NewValue);
})); #endregion #region 当前页 public int PageIndex
{
get
{
return (int)GetValue(PageIndexProperty);
}
set
{
SetValue(PageIndexProperty, value);
}
} // Using a DependencyProperty as the backing store for PageIndex. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageIndexProperty =
DependencyProperty.Register("PageIndex", typeof(int), typeof(Pager), new FrameworkPropertyMetadata(0,
(obj, e) =>
{
var pager = obj as Pager; if (pager == null) return; pager.SetPageIndex((int)e.NewValue); })); #endregion #region 总页数 public int TotalPage
{
get
{
return (int)GetValue(TotalPageProperty);
}
set
{
SetValue(TotalPageProperty, value);
}
} // Using a DependencyProperty as the backing store for TotalPage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TotalPageProperty =
DependencyProperty.Register("TotalPage", typeof(int), typeof(Pager), new FrameworkPropertyMetadata(0,
(obj, e) =>
{
var pager = obj as Pager; if (pager == null) return; pager.SetTotalPage((int)e.NewValue);
})); #endregion #endregion #region 路由事件 public static readonly RoutedEvent PageChangedEvent = EventManager.RegisterRoutedEvent("PageChanged",
RoutingStrategy.Bubble, typeof(EventHandler<PageChangedEventArg>), typeof(Pager)); public event EventHandler<PageChangedEventArg> PageChanged
{
add
{
this.AddHandler(PageChangedEvent, value);
}
remove
{
this.RemoveHandler(PageChangedEvent, value);
}
} protected virtual void OnPageChanged()
{
var pageChangedEventArg = new PageChangedEventArg(PageIndex);
pageChangedEventArg.RoutedEvent = PageChangedEvent;
pageChangedEventArg.Source = this;
this.RaiseEvent(pageChangedEventArg);
} #endregion #region 方法 public void SetTotalPage(int totalPage)
{
_txtTotalPage.Text = totalPage.ToString();
} public void SetPageIndex(int pageIndex)
{
_txtPageIndex.Text = pageIndex.ToString();
} public void SetPageSize(int pageSize)
{
_txtPageSize.Text = pageSize.ToString();
} /// <summary>
/// 设置总记录数
/// </summary>
/// <param name="totalCount"></param>
public void SetTotalCount(int totalCount)
{
_txtTotalCount.Text = totalCount.ToString();
} public override void OnApplyTemplate()
{
base.OnApplyTemplate(); _txtTotalCount = GetTemplateChild("PART_TotalCount") as TextBlock;
_txtPageSize = GetTemplateChild("PART_PageSize") as TextBlock;
_txtPageIndex = GetTemplateChild("PART_PageIndex") as TextBlock;
_txtTotalPage = GetTemplateChild("PART_TotalPage") as TextBlock; _btnFirstPage = GetTemplateChild("PART_FirstPage") as Button;
_btnPrePage = GetTemplateChild("PART_PrePage") as Button;
_btnNextPage = GetTemplateChild("PART_NextPage") as Button;
_btnLastPage = GetTemplateChild("PART_LastPage") as Button;
_btnPageGo = GetTemplateChild("PART_PageGo") as Button; _btnFirstPage.Click += _btnFirstPage_Click;
_btnPrePage.Click += _btnPrePage_Click;
_btnNextPage.Click += _btnNextPage_Click;
_btnLastPage.Click += _btnLastPage_Click;
_btnPageGo.Click += _btnPageGo_Click; _txtBoxPageNum = GetTemplateChild("PART_PageNum") as TextBox;
} #endregion #region 私有方法
private void _btnPageGo_Click(object sender, RoutedEventArgs e)
{
var pageNum = 1; if (int.TryParse(_txtBoxPageNum.Text, out pageNum))
{
if (pageNum >= 1 && pageNum <= TotalPage)
{
PageIndex = pageNum; OnPageChanged();
}
}
} private void _btnLastPage_Click(object sender, RoutedEventArgs e)
{
PageIndex = TotalPage; OnPageChanged();
} private void _btnNextPage_Click(object sender, RoutedEventArgs e)
{
if (PageIndex < TotalPage)
{
PageIndex++; OnPageChanged();
}
} private void _btnPrePage_Click(object sender, RoutedEventArgs e)
{
if (PageIndex > 1)
{
PageIndex--; OnPageChanged();
}
} private void _btnFirstPage_Click(object sender, RoutedEventArgs e)
{
PageIndex = 1; OnPageChanged();
} #endregion
}

事件参数 PageChangedEventArg.cs

public class PageChangedEventArg : RoutedEventArgs
{
public int PageIndex
{
get; set;
} public PageChangedEventArg(int pageIndex) : base()
{
PageIndex = pageIndex;
}
}

Demo和源码都在开源中国上

WPF自定义分页控件,样式自定义,简单易用的更多相关文章

  1. WPF Calendar 日历控件 样式自定义

    原文:WPF Calendar 日历控件 样式自定义 粗略的在代码上做了些注释 blend 生成出来的模版 有的时候 会生成 跟 vs ui界面不兼容的代码 会导致可视化设计界面 报错崩溃掉 但是确不 ...

  2. WPF 4 DataGrid 控件(自定义样式篇)

    原文:WPF 4 DataGrid 控件(自定义样式篇)      在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...

  3. asp.net webform 自定义分页控件

    做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件. 翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册. 有图有真相,给个直观的认识: 自定义分页控件前台代码: & ...

  4. arcgis api for js共享干货系列之二自定义Navigation控件样式风格

    arcgis api for js默认的Navigation控件样式风格如下图: 这样的风格不能说不好,各有各的爱好,审美观,这里也不是重点,这里的重点是如何自定义一套自己喜欢的样式风格呢:自己自定义 ...

  5. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  6. Mvc自定义分页控件

    MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...

  7. arcgis api 3.x for js 共享干货系列之二自定义 Navigation 控件样式风格(附源码下载)

    0.内容概览 自定义 Navigation 控件样式风格 源码下载 1.内容讲解 arcgis api 3.x for js 默认的Navigation控件样式风格如下图:这样的风格不能说不好,各有各 ...

  8. 抛砖引玉 【镜像控件】 WPF实现毛玻璃控件不要太简单

    原文:抛砖引玉 [镜像控件] WPF实现毛玻璃控件不要太简单 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/articl ...

  9. WPF设置全局控件样式

    原文:WPF设置全局控件样式 方法: 在资源文件APP.XAML中添加如下资源 <Application x:Class="_360UI.App" xmlns="h ...

随机推荐

  1. centos7安装与配置ansible

    此次测试总共有三台机,分别如下: ansible服务器:10.0.0.20 client01:10.0.0.21 client02:10.0.0.22 一.安装ansible 1. python版本需 ...

  2. new LayoutParams 使用

    ImageView imageView = new ImageView(mcontext); LayoutParams layoutParams = new LayoutParams(150,130) ...

  3. MySql数据查询的逻辑蕴含条件问题

    SQL语言中没有蕴含逻辑运算.但是,可以利用谓词演算将一个逻辑蕴含的谓词等价转换为:p->q ≡┐p∨q. 我们通过一个具体的题目来分析:(具体的表和数据详见文章:Mysql数据库中的EXIST ...

  4. ObservableCollection 分组后排序报错问题

    ObservableCollection通过Move方法可以移动顺序,如下: 将ObservableCollection中的一个item置顶: private ObservableCollection ...

  5. 第5季-小试牛刀-项目开发\阶段2-新手上路\项目-移动物体监控系统\Sprint0-产品设计与规划

    lesson1---产品功能展示 先完成准备阶段,准备阶段要做的事情: a.项目经理选择团队, b.根据项目用户需求以及同类型的实物,制定产品功能列表 c.根据功能的难易程度,制定迭代周期以及在每周期 ...

  6. Ubuntu14跑DSO

    按照https://github.com/JakobEngel/dso上的说明,make -j4的时候出现一下错误: /home/zhao/dso/src/FullSystem/CoarseIniti ...

  7. [java基础]short s1 = 1; s1 = s1 + 1;有什么错?short s1 = 1; s1 += 1;有什么错?

    为什么写这篇文章是因为搜到的答案里并没有阐明s1 = s1 + 1为什么就要转换为int类型. 由一下实验可知: public class test { public static void main ...

  8. 虚拟创建失败之Dbus调试

    DBus调试命令 查询连接名 ### 查询所有连接名 # dbus-send --system --print-reply --dest=org.freedesktop.DBus / org.free ...

  9. MySQL的CURRENT_DATE(),NOW(),DATE_FORMAT()函数的应用,类型转换及操作汇总

    内容简介 datetime类型和timestamp类型的取值范围,CURRENT_DATE()函数应用,NOW()函数应用,DATE_FROMAT()函数取星期,日期.字符串.时间戳相互转换等. 时间 ...

  10. ElasticSearch 学习一: 基本命令

    1. 启动时指定集群和节点的名字./elasticsearch -Ecluster.name=my_cluster_name -Enode.name=my_node_namee.g. ./elasti ...