WPF中的用户控件(UserControl)
前台代码
<UserControl x:Class="Layout.UI.Comm.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" mc:Ignorable="d" > <Grid> <Label Content="" Height="28" HorizontalAlignment="Left" Margin="0,0,0,0" Name="lblinfo" VerticalAlignment="Center" /> <Button Content="上一页" Height="23" HorizontalAlignment="Left" Margin="172,0,0,0" Name="btnPrev" VerticalAlignment="Center" Width="75" Click="btnPrev_Click" /> <Button Content="下一页" Height="23" HorizontalAlignment="Right" Margin="0,0,272,0" Name="btnNext" VerticalAlignment="Center" Width="75" Click="btnNext_Click"/> <TextBox Height="23" Margin="533,0,0,0" Name="txtCurrentPage" VerticalAlignment="Center" HorizontalAlignment="Left" Width="34" /> <Button Content="转到" HorizontalAlignment="Right" Margin="0,0,96,0" Name="btnGo" Width="75" Height="23" Click="btnGo_Click" /> <Label Content="页" HorizontalAlignment="Left" Margin="573,0,0,0" Name="label2" VerticalAlignment="Center" /> <Label Content="第" Margin="0,0,68,0" Name="label3" VerticalAlignment="Center" HorizontalAlignment="Right" /> <Button Content="首页" Height="23" HorizontalAlignment="Left" Margin="91,0,0,0" Name="btnFirst" VerticalAlignment="Center" Width="75" Click="btnFirst_Click" /> <Button Content="末页" Height="23" HorizontalAlignment="Left" Margin="334,0,0,0" Name="btnLast" VerticalAlignment="Center" Width="75" Click="btnLast_Click" /> </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; namespace Layout.UI.Comm { /// <summary> /// 申明委托 /// </summary> /// <param name="e"></param> /// <returns></returns> public delegate int EventPagingHandler(EventPagingArg e); /// <summary> ///wpf分页控件.钟健2011年4月7日16:38:06 /// </summary> /// public partial class Pager : UserControl { public Pager() { InitializeComponent(); } public event EventPagingHandler EventPaging; /// <summary> /// 每页显示记录数 /// </summary> private int _pageSize = 20; /// <summary> /// 每页显示记录数 /// </summary> public int PageSize { get { return _pageSize; } set { _pageSize = value; GetPageCount(); } } private int _nMax = 0; /// <summary> /// 总记录数 /// </summary> public int NMax { get { return _nMax; } set { _nMax = value; GetPageCount(); } } private int _pageCount = 0; /// <summary> /// 页数=总记录数/每页显示记录数 /// </summary> public int PageCount { get { return _pageCount; } set { _pageCount = value; } } private int _pageCurrent = 0; /// <summary> /// 当前页号 /// </summary> public int PageCurrent { get { return _pageCurrent; } set { _pageCurrent = value; } } private void GetPageCount() { if (this.NMax > 0) { this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize))); } else { this.PageCount = 0; } } /// <summary> /// 翻页控件数据绑定的方法 /// </summary> public void Bind() { if (this.EventPaging != null) { this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent)); } if (this.PageCurrent > this.PageCount) { this.PageCurrent = this.PageCount; } if (this.PageCount == 1) { this.PageCurrent = 1; } lblinfo.Content = ""+NMax+"条 " + this.PageCurrent.ToString() + "/" + this.PageCount.ToString()+""; this.txtCurrentPage.Text = this.PageCurrent.ToString(); if (this.PageCurrent == 1) { this.btnPrev.IsEnabled = false; this.btnFirst.IsEnabled = false; } else { btnPrev.IsEnabled = true; btnFirst.IsEnabled = true; } if (this.PageCurrent == this.PageCount) { this.btnLast.IsEnabled = false; this.btnNext.IsEnabled = false; } else { btnLast.IsEnabled = true; btnNext.IsEnabled = true; } if (this.NMax == 0) { btnNext.IsEnabled = false; btnLast.IsEnabled = false; btnFirst.IsEnabled = false; btnPrev.IsEnabled = false; } } private void btnLast_Click(object sender, RoutedEventArgs e) { PageCurrent = PageCount; this.Bind(); } private void btnNext_Click(object sender, RoutedEventArgs e) { this.PageCurrent += 1; if (PageCurrent > PageCount) { PageCurrent = PageCount; } this.Bind(); } private void btnGo_Click(object sender, RoutedEventArgs e) { if (this.txtCurrentPage.Text != null && txtCurrentPage.Text != "") { if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent)) { this.Bind(); } else { MessageBox.Show("输入数字格式错误!"); } } } private void btnFirst_Click(object sender, RoutedEventArgs e) { PageCurrent = 1; this.Bind(); } private void btnPrev_Click(object sender, RoutedEventArgs e) { PageCurrent -= 1; if (PageCurrent <= 0) { PageCurrent = 1; } this.Bind(); } } /// <summary> /// 自定义事件数据基类 /// </summary> public class EventPagingArg : EventArgs { private int _intPageIndex; public EventPagingArg(int PageIndex) { _intPageIndex = PageIndex; } } }
代码写好之后在主页面调用该用户控件:
在主页面中<Window>中加入 xmlns:local="clr-namespace:用户控件命名空间"
然后只需在页面代码中嵌入 <local:用户控件名></local:用户控件名>即可使用
主页面后台调用代码:
privatevoid Form_Loaded(object sender, RoutedEventArgs e) { pager.PageSize =12; pager.PageCurrent =1; BindData(); pager.NMax = total; } string strWhere ="IsPass=1"; int total =0; DataSet ds; privatevoid BindData() { ds = OrderRecords.instance.GetList(pager.PageSize, pager.PageCurrent, strWhere, "Status asc,CurTime Desc", out total); gvOrderList.ItemsSource = ds.Tables[0].DefaultView; gvOrderList.CanUserAddRows =false; } privateint pager_EventPaging(Comm.EventPagingArg e) { int pagd = pager.PageCurrent; BindData(); return total; }
附:在xaml页面引用(.dll)地址: