自定义控件DataPager
在最近的一个项目中,涉及到一个自定义翻页控制的控件,下面就这一个控件做详细的说明,这个自定义控件的主要作用是对数据源进行翻页控制,比如说:“上一页、下一页、首页、末页”等相关操作,由于在一个项目中有多个界面要用到这一部分,所以我们将其封装成一个自定义控件,从而使软件整体结构更加清楚明了,首先我们来将控件的界面展示出来。
整个控件可以分成三个部分,最左边是一个Combobox控件,中间是常见的上一页、下一页、向前、向后操作,最右边是显示当前记录。下面我们重点来介绍中间部分的封装,在这里我们将每一个按钮封装成一个ImageButton控件,这里我们贴出相应的前台、后台代码,从而从整体上进行分析。
- <Button x:Class="CustomControlLibrary.ImageButton"
- 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"
- mc:Ignorable="d" x:Name="loc"
- d:DesignHeight="44" d:DesignWidth="75">
- <Button.Resources>
- <ResourceDictionary Source="Themes/Generic.xaml" />
- </Button.Resources>
- <Image Name="innerImage" Source="{Binding ImageSource,ElementName=loc}" Stretch="None" />
- </Button>
这里前台的代码比较简单,我们将Button的样式写在了一个资源字典中,同时在Button上面放置一个Image控件,从而组成我们的ImageButton控件,在后台的代码中我们主要定义了两个依赖项属性,ImageSource和GrayImageSource,ImageSource是当按钮正常使用的时候,其显示的背景,GrayImageSource是当按钮的IsEnable=false的时候,其显示的背景,这里先贴出其后台代码。
- 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 CustomControlLibrary {
- /// <summary>
- /// ImageButton.xaml 的交互逻辑
- /// </summary>
- public partial class ImageButton : Button
- {
- public ImageButton()
- {
- InitializeComponent();
- this.Style = this.FindResource("ImageButtonStyle") as Style;
- this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(ImageButton_IsEnabledChanged);
- }
- void ImageButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- if (this.IsEnabled && ImageSource != null)
- {
- innerImage.Source = ImageSource;
- } else if (!this.IsEnabled && GrayImageSource != null)
- {
- innerImage.Source = GrayImageSource;
- }
- }
- public ImageSource ImageSource
- {
- get { return (ImageSource)GetValue(ImageSourceProperty); }
- set { SetValue(ImageSourceProperty, value); }
- }
- // Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty ImageSourceProperty =
- DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
- public ImageSource GrayImageSource
- {
- get { return (ImageSource)GetValue(GrayImageSourceProperty); }
- set { SetValue(GrayImageSourceProperty, value); }
- }
- // Using a DependencyProperty as the backing store for GrayImageSource. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty GrayImageSourceProperty =
- DependencyProperty.Register("GrayImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- if (this.IsEnabled && ImageSource != null)
- {
- innerImage.Source = ImageSource;
- } else if (!this.IsEnabled && GrayImageSource != null)
- {
- innerImage.Source = GrayImageSource;
- }
- }
- }
- }
这里首先分析一下WPF的依赖项属性和普通的.net属性有什么不同,依赖属性是WPF平台中一个新增的概念,出现的目的是用来实现WPF中的样式、自动绑定及实现动画等特性。依赖属性的出现是WPF这种特殊的呈现原理派生出来的,与.NET普通属性不同的是,依赖属性的值是依靠多个提供程序来判断的,并且其具有内建的传递变更通知的能力。
依赖属性的定义与普通的.NET属性的定义有区别,普通的.NET属性定义只需要定义其set和get区块的赋值与设置即可,而依赖属性需要向.NET的属性系统进行注册。这里我们定义了ImageSource和GrayImageSource两个依赖项属性。另外这里还重写了基类的OnApplyTemplate方法,这个方法是FrameworkElement中定义的方法,在派生类中重写后,每当应用程序代码或内部进程调用 ApplyTemplate,都将调用此方法。FrameworkElement 派生类可以使用 OnApplyTemplate 类处理程序获知显式调用或由布局系统调用此方法的情况。OnApplyTemplate 在完全生成模板并将其附加到逻辑树之后调用。
然后就是具体的DataPager控件的实现了,这里也分别贴出相应的前台和后台的代码。
- <UserControl x:Class="CustomControlLibrary.DataPager" 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:loc="clr-namespace:CustomControlLibrary"
- x:Name="dp" Margin="3"
- mc:Ignorable="d"
- d:DesignHeight="46" d:DesignWidth="590" Loaded="DataPager_Loaded">
- <Grid>
- <!--<Grid.Resources>
- <Style TargetType="{x:Type Image}">
- <Setter Property="Margin" Value="3,0,3,0" />
- <Setter Property="Cursor" Value="Hand" />
- </Style>
- </Grid.Resources>-->
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto" />
- <ColumnDefinition />
- <ColumnDefinition Width="Auto" />
- </Grid.ColumnDefinitions>
- <ComboBox Grid.Column="0" VerticalAlignment="Center" Name="cboPageSize"
- MinWidth="40" Margin="5,0,0,0" ItemsSource="{Binding Path=PageSizeItems,ElementName=dp}"
- SelectedItem="{Binding PageSize,Mode=TwoWay,ElementName=dp}" SelectionChanged="cbpPageSize_SelectionChanged" Visibility="Visible"/>
- <StackPanel Grid.Column="1" VerticalAlignment="Center" Orientation="Horizontal" Margin="5,0,0,0">
- <loc:ImageButton Click="btnFirst_Click" x:Name="btnFirst"
- ImageSource="/CustomControlLibrary;component/Images/pagination_first.gif"
- GrayImageSource="/CustomControlLibrary;component/Images/pagination_first_gray.gif" />
- <loc:ImageButton Click="btnPrev_Click" x:Name="btnPrev"
- ImageSource="/CustomControlLibrary;component/Images/pagination_prev.gif"
- GrayImageSource="/CustomControlLibrary;component/Images/pagination_prev_gray.gif" />
- <TextBlock Text="第页 " VerticalAlignment="Center"/>
- <TextBox Width="30" Text="{Binding Path=PageIndex,ElementName=dp}" Name="tbPageIndex" PreviewKeyDown="tbPageIndex_PreviewKeyDown" LostFocus="tbPageIndex_LostFocus" />
- <TextBlock Text=" 共 " VerticalAlignment="Center"/>
- <TextBlock Text="{Binding Path=PageCount, ElementName=dp}" VerticalAlignment="Center"/>
- <loc:ImageButton Click="btnNext_Click" x:Name="btnNext"
- ImageSource="/CustomControlLibrary;component/Images/pagination_next.gif"
- GrayImageSource="/CustomControlLibrary;component/Images/pagination_next_gray.gif" />
- <loc:ImageButton Click="btnLast_Click" x:Name="btnLast"
- ImageSource="/CustomControlLibrary;component/Images/pagination_last.gif"
- GrayImageSource="/CustomControlLibrary;component/Images/pagination_last_gray.gif" />
- <!--<loc:ImageButton Click="btnRefresh_Click"
- ImageSource="/CustomControlLibrary;component/Images/pagination_load.png" Visibility="Hidden"/>-->
- </StackPanel>
- <TextBlock Grid.Column="2" VerticalAlignment="Center" Margin="5,0,5,0" >显示记录
- <TextBlock Text="{Binding Path=Start,ElementName=dp}" /> 到 <TextBlock Text="{Binding Path=End,ElementName=dp}"/> 共 <TextBlock Text="{Binding Path=Total,ElementName=dp}"/> 条记录
- </TextBlock>
- </Grid>
- </UserControl>
这里重点介绍后台的逻辑代码。
- 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;
- using System.ComponentModel;
- namespace CustomControlLibrary {
- /// <summary>
- /// DataPager.xaml 的交互逻辑
- /// </summary>
- public partial class DataPager : UserControl, INotifyPropertyChanged
- {
- public DataPager()
- {
- InitializeComponent();
- }
- #region 依赖属性和事件
- public string PageSizeList
- {
- get { return (string)GetValue(PageSizeListProperty); }
- set { SetValue(PageSizeListProperty, value); }
- }
- // Using a DependencyProperty as the backing store for PageSizeList. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty PageSizeListProperty =
- DependencyProperty.Register("PageSizeList", typeof(string), typeof(DataPager), new UIPropertyMetadata("5,10,20", (s, e) =>
- {
- DataPager dp = s as DataPager;
- if (dp.PageSizeItems == null)
- dp.PageSizeItems = new List<int>();
- else
- dp.PageSizeItems.Clear();
- dp.RaisePropertyChanged("PageSizeItems");
- }));
- 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(DataPager), new UIPropertyMetadata(16));
- public int Total
- {
- get { return (int)GetValue(TotalProperty); }
- set { SetValue(TotalProperty, value); }
- }
- // Using a DependencyProperty as the backing store for Total. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty TotalProperty =
- DependencyProperty.Register("Total", typeof(int), typeof(DataPager), new UIPropertyMetadata(0));
- 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(DataPager), new UIPropertyMetadata(1));
- public IEnumerable<object> ItemsSource
- {
- get { return (IEnumerable<object>)GetValue(ItemsSourceProperty); }
- set { SetValue(ItemsSourceProperty, value); }
- }
- /// <summary>
- /// ItemsSource数据源
- /// </summary>
- public static readonly DependencyProperty ItemsSourceProperty =
- DependencyProperty.Register("ItemsSource", typeof(IEnumerable<object>), typeof(DataPager), new UIPropertyMetadata(null));
- public static readonly RoutedEvent PageChangedEvent = EventManager.RegisterRoutedEvent("PageChanged", RoutingStrategy.Bubble, typeof(PageChangedEventHandler), typeof(DataPager));
- /// <summary>
- /// 分页更改事件
- /// </summary>
- public event PageChangedEventHandler PageChanged
- {
- add
- {
- AddHandler(PageChangedEvent, value);
- }
- remove
- {
- RemoveHandler(PageChangedEvent, value);
- }
- }
- #endregion
- #region 通知属性
- private List<int> _pageSizeItems;
- /// <summary>
- /// 显示每页记录数集合
- /// </summary>
- public List<int> PageSizeItems
- {
- get
- {
- if (_pageSizeItems == null)
- {
- _pageSizeItems = new List<int>();
- }
- if (PageSizeList != null)
- {
- List<string> strs = PageSizeList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
- _pageSizeItems.Clear();
- strs.ForEach(c => {
- _pageSizeItems.Add(Convert.ToInt32(c));
- });
- }
- return _pageSizeItems;
- }
- set
- {
- if (_pageSizeItems != value)
- {
- _pageSizeItems = value;
- RaisePropertyChanged("PageSizeItems");
- }
- }
- }
- private int _pageCount;
- /// <summary>
- /// 总页数
- /// </summary>
- public int PageCount
- {
- get { return _pageCount; }
- set
- {
- if (_pageCount != value)
- {
- _pageCount = value;
- RaisePropertyChanged("PageCount");
- }
- }
- }
- private int _start;
- /// <summary>
- /// 开始记录数
- /// </summary>
- public int Start
- {
- get { return _start; }
- set
- {
- if (_start != value)
- {
- _start = value;
- RaisePropertyChanged("Start");
- }
- }
- }
- private int _end;
- /// <summary>
- /// 结束记录数
- /// </summary>
- public int End
- {
- get { return _end; }
- set
- {
- if (_end != value)
- {
- _end = value;
- RaisePropertyChanged("End");
- }
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public void RaisePropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- #endregion
- #region 字段、属性、委托
- public delegate void PageChangedEventHandler(object sender, PageChangedEventArgs args);
- private PageChangedEventArgs pageChangedEventArgs;
- #endregion
- #region 引发分页更改事件
- /// <summary>
- /// 引发分页更改事件
- /// </summary>
- private void RaisePageChanged()
- {
- if (pageChangedEventArgs == null)
- {
- pageChangedEventArgs = new PageChangedEventArgs(PageChangedEvent, PageSize, PageIndex);
- }
- else
- {
- pageChangedEventArgs.PageSize = this.PageSize;
- pageChangedEventArgs.PageIndex = this.PageIndex;
- }
- RaiseEvent(pageChangedEventArgs);
- //calc start、end
- if (ItemsSource != null)
- {
- int curCount = ItemsSource.Count();
- Start = (PageIndex - 1) * PageSize + 1;
- End = Start + curCount - 1;
- if (Total % PageSize != 0)
- {
- PageCount = Total / PageSize + 1;
- }
- else
- {
- PageCount = Total / PageSize;
- }
- }
- else
- {
- Start = End = PageCount = Total = 0;
- }
- //调整图片的显示
- btnFirst.IsEnabled = btnPrev.IsEnabled = (PageIndex != 1);
- btnNext.IsEnabled = btnLast.IsEnabled = (PageIndex != PageCount);
- }
- #endregion
- #region 分页操作事件
- void DataPager_Loaded(object sender, RoutedEventArgs e)
- {
- RaisePageChanged();
- }
- private void cbpPageSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (this.IsLoaded)
- {
- PageSize = (int)cboPageSize.SelectedItem;
- RaisePageChanged();
- }
- }
- private void btnFirst_Click(object sender, RoutedEventArgs e)
- {
- PageIndex = 1;
- RaisePageChanged();
- }
- private void btnPrev_Click(object sender, RoutedEventArgs e)
- {
- if (PageIndex > 1)
- {
- --PageIndex;
- }
- RaisePageChanged();
- }
- private void btnNext_Click(object sender, RoutedEventArgs e)
- {
- if (Total % PageSize != 0)
- {
- PageCount = Total / PageSize + 1;
- }
- else
- {
- PageCount = Total / PageSize;
- }
- if (PageIndex < PageCount)
- {
- ++PageIndex;
- }
- RaisePageChanged();
- }
- private void btnLast_Click(object sender, RoutedEventArgs e)
- {
- if (Total % PageSize != 0)
- {
- PageCount = Total / PageSize + 1;
- }
- else
- {
- PageCount = Total / PageSize;
- }
- PageIndex = PageCount;
- RaisePageChanged();
- }
- private void btnRefresh_Click(object sender, RoutedEventArgs e)
- {
- RaisePageChanged();
- }
- private void tbPageIndex_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- tbPageIndex_LostFocus(sender, null);
- }
- }
- private void tbPageIndex_LostFocus(object sender, RoutedEventArgs e)
- {
- int pIndex = 0;
- try
- {
- pIndex = Convert.ToInt32(tbPageIndex.Text);
- } catch { pIndex = 1; }
- if (pIndex < 1) PageIndex = 1;
- else if (pIndex > PageCount) PageIndex = PageCount;
- else PageIndex = pIndex;
- RaisePageChanged();
- }
- #endregion
- }
- /// <summary>
- /// 分页更改参数
- /// </summary>
- public class PageChangedEventArgs : RoutedEventArgs
- {
- public int PageSize { get; set; }
- public int PageIndex { get; set; }
- public PageChangedEventArgs(RoutedEvent routeEvent, int pageSize, int pageIndex)
- : base(routeEvent)
- {
- this.PageSize = pageSize;
- this.PageIndex = pageIndex;
- }
- }
- }
这里定义了一些该控件要使用的一些属性,PageSize属性用于定义每一页中的项的数量,Total属性定义总共有多少项,PageIndex定义当前的页码,ItemsSource用于定义数据源的集合,类型是IEnumerable<object>类型,这个里面定义的最重要的函数是RaisePageChanged,该函数将引发分页更改事件,在这个函数中我们将当前定义的路由事件PageChangedEvent,以及每一页项目的大小,以及当前页码传递到PageChangedEventArgs的一个对象中,然后调用RaiseEvent来引发特定的路由事件,这里需要注意的是必须将当前定义的路由事件传递到基类RoutedEventArgs中,否则不能触发该路由事件。
自定义控件DataPager的更多相关文章
- android自定义控件一站式入门
自定义控件 Android系统提供了一系列UI相关的类来帮助我们构造app的界面,以及完成交互的处理. 一般的,所有可以在窗口中被展示的UI对象类型,最终都是继承自View的类,这包括展示最终内容的非 ...
- ASP.NET MVC学习之母版页和自定义控件的使用
一.母板页_Layout.cshtml类似于传统WebForm中的.master文件,起到页面整体框架重用的目地1.母板页代码预览 <!DOCTYPE html> <html> ...
- C# 自定义控件VS用户控件
1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Con ...
- 自定义控件之 圆形 / 圆角 ImageView
一.问题在哪里? 问题来源于app开发中一个很常见的场景——用户头像要展示成圆的: 二.怎么搞? 机智的我,第一想法就是,切一张中间圆形透明.四周与底色相同.尺寸与头像相同的蒙板图片,盖在 ...
- 如何开发FineReport的自定义控件?
FineReport作为插件化开发的报表软件,有些特殊需求的功能需要自己开发,开发的插件包帆软官方有提提供,可以去帆软论坛上找,本文将主要介绍如何开发一个自定义控件,这里讲讲方法论. 第一步:实例化一 ...
- WPF自定义控件第二 - 转盘按钮控件
继之前那个控件,又做了一个原理差不多的控件.这个控件主要模仿百度贴吧WP版帖子浏览界面左下角那个弹出的按钮盘.希望对大家有帮助. 这个控件和之前的也差不多,为了不让大家白看,文章最后发干货. 由于这个 ...
- 【Win 10应用开发】AdaptiveTrigger在自定义控件中是可以触发的
前些天,看到有网友给我留言,说AdaptiveTrigger在自定义控件(模板化控件)中不能触发.因为当时我正在写其他的代码,就没有去做实验来验证,于是我就给这位网友提了使用GotoVisualSta ...
- WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...
- Android自定义控件之自定义ViewGroup实现标签云
前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...
随机推荐
- undefined == false 么
今天碰到个问题,我需要去判断 undefined == false 会返回什么,想当然的以为会返回true,但是结果却返回的是false,这我就有点晕了,不是说undefined.null.0.NaN ...
- 如何控制docker的CPU和内存份额
1.内存:docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 500M 刚开始会报错: docker ...
- 11.11 开课二个月零七天(ajax和bootstrp做弹窗)
1.用ajax做弹窗显示信息详情 nation.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...
- 【SCOI2015】小凸想跑步
题面 题解 推波柿子: 设点\(A(x_a, y_a), B(x_b, y_b), C(x_c, y_c), D(x_d, y_d), P(x, y)\) \(\vec{a} = (x_b - x_a ...
- 【URLOS开发入门】docker官方系统镜像——Alpine入门教程
我们在进行URLOS应用开发时,经常会用到一些基础系统镜像,如:ubuntu.CentOS.Debian等,我们可以通过docker pull命令直接拉取官方镜像. root@ubuntu:~# do ...
- GlusterFS分布式存储数据的恢复机制(AFR)的说明
GlusterFSFS恢复数据都是基于副本卷来说的,GlusterFSFS复制卷是采用镜像的方式做的,并且是同步事务性操作.简单来说就是,某一个客户要写文件时,先把这个文件锁住,然后同时写两个或多个副 ...
- Samba服务的配置总结
之前介绍了Linux下Samba服务器部署,这里简单总结下Samba服务参数的配置说明: Samba服务的主配置文件是smb.conf,默认在/etc/samba/目录下.smb.conf含有多个段, ...
- uml 图学习记录
UML类图与类的关系详解 2011-04-21 来源:网络 在画类图的时候,理清类和类之间的关系是重点.类的关系有泛化(Generalization).实现(Realization).依赖(D ...
- oracle数据库添加新用户
/*分为四步 */ /*第1步:创建临时表空间 */ create temporary tablespace kmyf_temp tempfile 'E:\app\pangxy\product\11. ...
- Linux内核分析第七周总结
第七章 可执行程序的装载 可执行程序的生成 可执行程序的生成: c语言代码--->经过编译器的预处理--->编译成汇编代码--->由汇编器编译成目标代码--->链接成可执行文件 ...