好多年没有发博了,今天闲暇,把给公司做的ERP分页控件拿出来,有很多都用到分页的,所以就封装了一个UserControl分页控件,效果还不错,可以看看,先!
注意:有用到存储过程。
如果还不明白,请联系我!
效果图:
1、分页实体类
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 /// <summary>
/// 分页实体类
/// </summary>
public class Pager
{
#region Private Member
private int pageIndex=1; //当前页,默认第一页
private int pageSize=20; //页大小,默认20条目
private int pageCount; //总共页
private int recorderCount; //总共条目
#endregion
#region Public Attribute
/// <summary>
/// 当前页
/// </summary>
public int PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
}
/// <summary>
/// 页大小
/// </summary>
public int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
/// <summary>
/// 总共页
/// </summary>
public int PageCount
{
get
{
return recorderCount % pageSize > 0 ?
(recorderCount / pageSize) + 1 :
(recorderCount / pageSize);
}
set { pageCount = value; }
}
/// <summary>
/// 总共条目数
/// </summary>
public int RecorderCount
{
get { return recorderCount; }
set { recorderCount = value; }
}
#endregion
#region Initiliazation
/// <summary>
/// 无参构造
/// </summary>
public Pager() {}
/// <summary>
/// 有参构造
/// </summary>
/// <param name="pageSize"></param>
public Pager(int pageSize) {
this.pageSize = pageSize;
}
#endregion
}
2、界面设置
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <UserControl x:Class="RanchUCS.UCPager"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Height="25" Width="Auto">
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="130" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="txbInfo" HorizontalAlignment="Left" Height="15" Margin="10,0,0,0"></TextBlock>
<Image x:Name="iFirst" ToolTip="首页" IsEnabled="False" Height="12" HorizontalAlignment="Left" Source="../Resources/Images/Pager/first1.gif" PreviewMouseLeftButtonDown="iFirst_PreviewMouseLeftButtonDown" Grid.Column="2"/>
<Image x:Name="iPrev" ToolTip="上一页" IsEnabled="False" Height="12" HorizontalAlignment="Left" Source="../Resources/Images/Pager/previous1.gif" PreviewMouseLeftButtonDown="iPrev_PreviewMouseLeftButtonDown" Grid.Column="3"/>
<WrapPanel Grid.Column="4" HorizontalAlignment="Center" VerticalAlignment="Center" Width="130">
<TextBlock Text="| " VerticalAlignment="Center"/>
<wfi:WindowsFormsHost Width="50">
<wf:NumericUpDown x:Name="nudPageIndex" KeyUp="nudPageIndex_KeyUp"/>
</wfi:WindowsFormsHost>
<TextBlock x:Name="txbTotalPageCount" VerticalAlignment="Center"/>
<TextBlock x:Name="txbGO" VerticalAlignment="Center" FontWeight="Bold" Text=" GO" PreviewMouseLeftButtonDown="txbGO_PreviewMouseLeftButtonDown"/>
<TextBlock Text=" | " VerticalAlignment="Center"/>
</WrapPanel>
<Image x:Name="iNext" ToolTip="下一页" IsEnabled="False" Height="12" HorizontalAlignment="Left" Source="../Resources/Images/Pager/next1.gif" PreviewMouseLeftButtonDown="iNext_PreviewMouseLeftButtonDown" Grid.Column="5"/>
<Image x:Name="iLast" ToolTip="末页" IsEnabled="False" Height="12" HorizontalAlignment="Left" Source="../Resources/Images/Pager/last1.gif" PreviewMouseLeftButtonDown="iLast_PreviewMouseLeftButtonDown" Grid.Column="6"/>
</Grid>
</UserControl>
3、界面逻辑
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 /// <summary>
/// 申明委托
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public delegate void EventPagingHandler(EventPagingArg e);
/// <summary>
/// UCPaging.xaml 的交互逻辑
/// </summary>
public partial class UCPager : UserControl
{
#region Pirvate Member
public Pager pager;
public DataTable dt;
public event EventPagingHandler EventPaging;
#endregion
/// <summary>
/// 无参构造
/// </summary>
public UCPager()
{
InitializeComponent();
pager = new Pager();
}
/// <summary>
/// 得到数据
/// </summary>
/// <param name="tableName"></param>
/// <param name="fldName"></param>
/// <param name="fldSort"></param>
/// <param name="strCondition"></param>
/// <returns></returns>
public void Bind()
{
if (this.EventPaging != null)
this.EventPaging(new EventPagingArg());
if (dt == null || dt.Rows.Count == 0)
{
this.Visibility = Visibility.Collapsed;
return;
}
else
this.Visibility = Visibility.Visible;
//--控制
this.txbInfo.Text = "第1-" + dt.Rows.Count + "条 共" + pager.RecorderCount + "条 | 第" + pager.PageIndex + "页 共" + pager.PageCount + "页";
this.nudPageIndex.Minimum = 1;
this.nudPageIndex.Maximum = pager.PageCount;
this.nudPageIndex.Value = pager.PageIndex;
this.txbTotalPageCount.Text = " / "+pager.PageCount;
if (pager.PageCount > 1 && pager.PageCount > pager.PageIndex)
{
this.iNext.Source = new BitmapImage(new Uri("../Resources/Images/Pager/next2.gif", UriKind.Relative));
this.iLast.Source = new BitmapImage(new Uri("../Resources/Images/Pager/last2.gif", UriKind.Relative));
this.iNext.IsEnabled = true;
this.iLast.IsEnabled = true;
}
else
{
this.iNext.Source = new BitmapImage(new Uri("../Resources/Images/Pager/next1.gif", UriKind.Relative));
this.iLast.Source = new BitmapImage(new Uri("../Resources/Images/Pager/last1.gif", UriKind.Relative));
this.iNext.IsEnabled = false;
this.iLast.IsEnabled = false;
}
if (pager.PageIndex > 1 && pager.PageIndex <= pager.PageCount)
{
this.iFirst.IsEnabled = true;
this.iPrev.IsEnabled = true;
this.iFirst.Source = new BitmapImage(new Uri("../Resources/Images/Pager/first2.gif", UriKind.Relative));
this.iPrev.Source = new BitmapImage(new Uri("../Resources/Images/Pager/previous2.gif", UriKind.Relative));
}
else
{
this.iFirst.Source = new BitmapImage(new Uri("../Resources/Images/Pager/first1.gif", UriKind.Relative));
this.iPrev.Source = new BitmapImage(new Uri("../Resources/Images/Pager/previous1.gif", UriKind.Relative));
this.iFirst.IsEnabled = false;
this.iPrev.IsEnabled = false;
}
}
/// <summary>
/// 得到数据
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="fldName">要查询的字段(所有为*)</param>
/// <param name="fldSort">排序</param>
/// <param name="strCondition">Where条件</param>
/// <returns>返回DataTable,如要绑定控件,可自己转换成IList</returns>
public DataTable GetData(string tableName, string fldName, string fldSort,
string strCondition)
{
int pageCount = 0;
int recordCount = 0;
dt = PagingManage.GetDataSet(tableName, fldName, pager.PageSize, pager.PageIndex, fldSort,
strCondition, out pageCount, out recordCount);
pager.PageCount = pageCount;
pager.RecorderCount = recordCount;
return dt;
}
/// <summary>
/// 第一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void iFirst_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.pager.PageIndex = 1;
this.Bind();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void iPrev_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.pager.PageIndex--;
this.Bind();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void iNext_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.pager.PageIndex++;
this.Bind();
}
/// <summary>
/// 末页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void iLast_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.pager.PageIndex = pager.PageCount;
this.Bind();
}
/// <summary>
/// 确定导航到指定页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void nudPageIndex_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == System.Windows.Forms.Keys.Return)
{
this.pager.PageIndex = int.Parse(this.nudPageIndex.Value.ToString());
this.Bind();
}
}
/// <summary>
/// 确定导航
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txbGO_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.pager.PageIndex = int.Parse(this.nudPageIndex.Value.ToString());
this.Bind();
}
}
/// <summary>
/// 自定义事件数据基类
/// </summary>
public class EventPagingArg : EventArgs
{
public EventPagingArg() { }
}