本文介绍的是分页控件的改进,引用dukey's 的发布的控件 ,详细见原文: http://www.cnblogs.com/dukey/archive/2011/08/09/2132187.html
原文控件使用有两个问题:1,首次绑定的时候,不显示总页数和当前页数,需要点击一下按钮才显示,
2,控件中的"上一页",“下一页”等按钮布局在不同宽度下显示不够友好,
效果如下图:
针对第一个问题,仔细看了原作者的源码,发现初次绑定的时候,没有给显示总记录数的lable赋值,
于是,在取得总记录数后就给显示记录的控件的Text属性赋值,改进后的代码如下所示,第8行后是我添加进来的,
1 public int NMax 2 { 3 get { return _nMax; } 4 set 5 { 6 _nMax = value; 7 GetPageCount(); 8 lblinfo.Content = "" + NMax + "条 " + this.PageCurrent.ToString() + "/" + this.PageCount.ToString() + ""; 9 10 this.txtCurrentPage.Text = this.PageCurrent.ToString(); 11 } 12 }
针对第2个问题,原文中采用的是Grid布局,
1 <Grid> 2 <Label Content="" Height="28" HorizontalAlignment="Left" Margin="0,0,0,0" Name="lblinfo" VerticalAlignment="Center" /> 3 <Button Content="上一页" Height="23" HorizontalAlignment="Left" Margin="172,0,0,0" Name="btnPrev" VerticalAlignment="Center" Width="75" Click="btnPrev_Click" /> 4 <Button Content="下一页" Height="23" HorizontalAlignment="Right" Margin="0,0,272,0" Name="btnNext" VerticalAlignment="Center" Width="75" Click="btnNext_Click"/> 5 <TextBox Height="23" Margin="533,0,0,0" Name="txtCurrentPage" VerticalAlignment="Center" HorizontalAlignment="Left" Width="34" /> 6 <Button Content="转到" HorizontalAlignment="Right" Margin="0,0,96,0" Name="btnGo" Width="75" Height="23" Click="btnGo_Click" /> 7 <Label Content="页" HorizontalAlignment="Left" Margin="573,0,0,0" Name="label2" VerticalAlignment="Center" /> 8 <Label Content="第" Margin="0,0,68,0" Name="label3" VerticalAlignment="Center" HorizontalAlignment="Right" /> 9 <Button Content="首页" Height="23" HorizontalAlignment="Left" Margin="91,0,0,0" Name="btnFirst" VerticalAlignment="Center" Width="75" Click="btnFirst_Click" /> 10 <Button Content="末页" Height="23" HorizontalAlignment="Left" Margin="334,0,0,0" Name="btnLast" VerticalAlignment="Center" Width="75" Click="btnLast_Click" /> 11 </Grid>
改成StackPanel后的代码:
1 <StackPanel Orientation="Horizontal" Margin="0" Width="700"> 2 <Label Content="" Height="28" Margin="10,0,0,0" Name="lblinfo" VerticalAlignment="Center" /> 3 <Button Content="首页" Height="23" Margin="10,0" Name="btnFirst" VerticalAlignment="Center" Width="75" Click="btnFirst_Click" /> 4 <Button Content="上一页" Height="23" Margin="10,0" Name="btnPrev" VerticalAlignment="Center" Width="75" Click="btnPrev_Click" /> 5 <Button Content="下一页" Height="23" Margin="10,0" Name="btnNext" VerticalAlignment="Center" Width="75" Click="btnNext_Click"/> 6 <Button Content="末页" Height="23" Margin="10,0" x:Name="btnLast" Width="75" Click="btnLast_Click" VerticalAlignment="Center" /> 7 8 9 10 <Label Content="第" Margin="10,0,2,0" Name="label3" VerticalAlignment="Center" /> 11 <TextBox Height="23" Margin="2,0" Name="txtCurrentPage" VerticalAlignment="Center" Width="34" /> 12 <Label Content="页" Margin="2,0" Name="label2" /> 13 <Button Content="转到" Margin="2,0,10,0" Name="btnGo" Width="75" Height="23" Click="btnGo_Click" /> 14 </StackPanel>
完....