一个web的分页控件改成了silverlight分页控件,现与大家分享一下 因为本例子是继上之前随笔的Deom所以没有与数据库连结,分页只能在内存中分页 在项目中此控件与row_number()存储过程分页一起使用先看一下样式
使用分页后:
ComboBox与RadioButton在DataGrid应用操作数据库时会像
当选中一项时再按分页时会出现选中行的位置在另一页中出现空白行 解决办法:在数据绑定时让控件重新获取焦点且分页控件的作用方法 如图:
控件源码:
xaml:
Code
<StackPanel Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Margin="2,0,0,0" Orientation="Horizontal" x:Name="PageControl1" Height="30">
<Button x:Name="Btn_Head" Tag="1" Width="40" Click="Button_Click" Content="首页" Grid.Row="0" Grid.Column="0" FontSize="12" Margin="4,0,0,0" Height="22"/>
<Button x:Name="Btn_Previous" Width="40" Click="Button_Click" Content="上一页" Grid.Row="0" Grid.Column="1" FontSize="12" Margin="4,0,0,0" Height="22"/>
<Button x:Name="Btn_1" Width="25" Content="1" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_2" Width="25" Content="2" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_3" Width="25" Content="3" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_4" Width="25" Content="4" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_5" Width="25" Content="5" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_6" Width="25" Content="6" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_7" Width="25" Content="7" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_8" Width="25" Content="8" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_9" Width="25" Content="9" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_10" Width="25" Content="10" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_Next" Width="40" Content="下一页" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_End" Width="40" Click="Button_Click" Content="尾页" FontSize="12" Margin="2,0,0,0" Height="22"/>
<TextBlock Text="当前页:" Width="40" Margin="4,0,0,0" VerticalAlignment="Center" FontSize="12"/>
<TextBlock x:Name="NumberCount" Text="1" Width="20" Margin="2,0,0,0" VerticalAlignment="Center" FontSize="12"/>
<TextBlock Text="跳转至" Width="40" VerticalAlignment="Center" FontSize="12"></TextBlock>
<TextBox x:Name="TBText" Margin="2,0,0,0" Width="40" FontSize="12" Height="22"></TextBox>
<Button x:Name="btnGo" Click="Button_Click" Margin="2,0,0,0" Width="30" FontSize="12" Height="22" Tag="-1" Content="OK"></Button>
</StackPanel>
<StackPanel Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Margin="2,0,0,0" Orientation="Horizontal" x:Name="PageControl1" Height="30">
<Button x:Name="Btn_Head" Tag="1" Width="40" Click="Button_Click" Content="首页" Grid.Row="0" Grid.Column="0" FontSize="12" Margin="4,0,0,0" Height="22"/>
<Button x:Name="Btn_Previous" Width="40" Click="Button_Click" Content="上一页" Grid.Row="0" Grid.Column="1" FontSize="12" Margin="4,0,0,0" Height="22"/>
<Button x:Name="Btn_1" Width="25" Content="1" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_2" Width="25" Content="2" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_3" Width="25" Content="3" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_4" Width="25" Content="4" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_5" Width="25" Content="5" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_6" Width="25" Content="6" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_7" Width="25" Content="7" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_8" Width="25" Content="8" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_9" Width="25" Content="9" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_10" Width="25" Content="10" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_Next" Width="40" Content="下一页" Click="Button_Click" FontSize="12" Margin="2,0,0,0" Height="22"/>
<Button x:Name="Btn_End" Width="40" Click="Button_Click" Content="尾页" FontSize="12" Margin="2,0,0,0" Height="22"/>
<TextBlock Text="当前页:" Width="40" Margin="4,0,0,0" VerticalAlignment="Center" FontSize="12"/>
<TextBlock x:Name="NumberCount" Text="1" Width="20" Margin="2,0,0,0" VerticalAlignment="Center" FontSize="12"/>
<TextBlock Text="跳转至" Width="40" VerticalAlignment="Center" FontSize="12"></TextBlock>
<TextBox x:Name="TBText" Margin="2,0,0,0" Width="40" FontSize="12" Height="22"></TextBox>
<Button x:Name="btnGo" Click="Button_Click" Margin="2,0,0,0" Width="30" FontSize="12" Height="22" Tag="-1" Content="OK"></Button>
</StackPanel>
cs.
Code
#region 分页控件
#region 分页属性
int pageQty = 0;
/// <summary>
/// 总页数(必须参数)
/// </summary>
public int PageQty
{
get
{
return pageQty;
}
set
{
pageQty = value;
}
}
private int lastPageNO = 10;
/// <summary>
/// 当前分页导航最后一页数值,默认值为:10
/// (注:对该属性赋值前要求先对PageQty属性进行赋值)
/// </summary>
public int LastPageNO
{
get
{
return lastPageNO;
}
set
{
if (this.PageQty == 0)
throw (new Exception("必须先定义最大页数PageQty属性!"));
lastPageNO = value > 0 ? value : 1;
}
}
int intCurrentPageNO = 1;
/// <summary>
/// 当前页号(必须参数)
/// </summary>
public int CurrentPageNO
{
get
{
try
{
return intCurrentPageNO;
}
catch
{
return 1;
}
}
set
{
if (this.pageQty > 0)
{
this.SetShow(this.LastPageNO, value);
this.PageControl1.Visibility = Visibility.Visible;
}
else
{
this.PageControl1.Visibility = Visibility.Collapsed;
}
intCurrentPageNO = value;
}
}
private string mTargetControlID = string.Empty;
public String TargetControlID
{
get
{
return mTargetControlID;
}
set
{
mTargetControlID = value;
}
}
#endregion
#region 设置导航按钮显示逻辑
/// <summary>
/// 设置导航按钮显示逻辑
/// </summary>
/// <param name="linksCount">要显示的导航数字数</param>
/// <param name="currentNO">当前页号</param>
public void SetShow(int linksCount, int currentNO)
{
if (linksCount > 10 || linksCount < 5)
{
linksCount = 10;
}
if (linksCount > this.PageQty)
{
linksCount = this.PageQty;
}
for (int i = 10; (i - linksCount) > 0; i--)
{
((Button)this.FindName(string.Format("Btn_{0}", i))).Visibility = Visibility.Collapsed;
}
for (int i = 1; i <= linksCount; i++)
{
((Button)this.FindName(string.Format("Btn_{0}", i))).Visibility = Visibility.Visible;
}
#region 设置导航数字逻辑
if ((currentNO + 4) <= linksCount)//currentNO + 4 在 1 到 linksCount-4 之间
{
setNumberLogic(1, this.CurrentPageNO, linksCount);
}
else if ((currentNO + 4) > linksCount && (currentNO + 4) <= this.PageQty) // currentNO + 4 在 linksCount-4 到 PageQty 之间
{
setNumberLogic(currentNO - (linksCount - 5), this.CurrentPageNO, linksCount);
}
else //currentNO + 4 > PageQty
{
setNumberLogic(PageQty - linksCount + 1, this.CurrentPageNO, linksCount);
}
#endregion
#region 设置前后导航
if (currentNO == 1 && currentNO != PageQty)
{
//第一页 && 不是最后一页
this.Btn_Head.IsEnabled = false;
this.Btn_Previous.IsEnabled = false;
this.Btn_Next.IsEnabled = true;
this.Btn_End.IsEnabled = true;
this.Btn_Next.Tag = (currentNO + 1).ToString();
}
else if (currentNO != 1 && currentNO == PageQty)
{
//不是第一页 && 最后一页
this.Btn_Head.IsEnabled = true;
this.Btn_Previous.IsEnabled = true;
this.Btn_Next.IsEnabled = false;
this.Btn_End.IsEnabled = false;
this.Btn_Previous.Tag = (currentNO - 1).ToString();
}
else if (currentNO != 1 && currentNO != PageQty)
{
//不是第一页 && 不是最后一页
this.Btn_Head.IsEnabled = true;
this.Btn_Previous.IsEnabled = true;
this.Btn_Next.IsEnabled = true;
this.Btn_End.IsEnabled = true;
this.Btn_Next.Tag = (currentNO + 1).ToString();
this.Btn_Previous.Tag = (currentNO - 1).ToString();
}
else
{
this.Btn_Head.IsEnabled = false;
this.Btn_Previous.IsEnabled = false;
this.Btn_Next.IsEnabled = false;
this.Btn_End.IsEnabled = false;
}
#endregion
}
/// <summary>
/// 设置导航数字逻辑
/// </summary>
/// <param name="fromNO">显示的起始页码</param>
/// <param name="currentNO">当前页码</param>
/// <param name="linksCount">显示页码数量</param>
private void setNumberLogic(int fromNO, int currentNO, int linksCount)
{
for (int i = 1; i <= linksCount; i++)
{
string btn = string.Format("Btn_{0}", i.ToString());
Button linkBtn = (Button)this.FindName(btn);
if ((fromNO + i - 1) == currentNO)
{
linkBtn.Content = currentNO.ToString();
linkBtn.IsEnabled = false;
}
else
{
string txt = (fromNO + i - 1).ToString();
linkBtn.Content = string.Format("{0}", txt);
linkBtn.Tag = txt;
linkBtn.IsEnabled = true;
}
}
}
#endregion
#region 事件
private void Button_Click(object sender, RoutedEventArgs e)
{
Button bt = sender as Button;
int messageid = Convert.ToInt32(bt.Tag);
if (messageid > 0)
{
//按钮跳转
this.CurrentPageNO = messageid;
NumberCount.Text = CurrentPageNO.ToString();
}
else
{
//用户输入页码跳转
try
{
int p = int.Parse(this.TBText.Text.Trim());
if (p > 0)
{
this.CurrentPageNO = p;
NumberCount.Text = CurrentPageNO.ToString();
}
else
return;
}
catch
{
return;
}
}
ContentDataBind();
}
#endregion
//分页控件的使用方法
public void ContentDataBind()
{
List<student> students = SetStudentInfo();
ContentGrid.Focus();
this.ContentGrid.ItemsSource = null;
this.ContentGrid.ItemsSource = (from st in students select st).Skip(PageSize * (CurrentPageNO - 1)).Take(PageSize).ToList();
var Count = students.Count();
this.ContentGrid.UpdateLayout();
PageQty = (int)Math.Ceiling((Count + 0.0) / PageSize);
if (pageQty == 0)
{
CurrentPageNO = 1;
}
else
CurrentPageNO = CurrentPageNO > PageQty ? PageQty : CurrentPageNO;
Btn_End.Tag = PageQty;
//页数大于1时才显示分页控件 可以根据自己的需要
if (PageQty > 1)
{
PageControl1.Visibility = Visibility.Visible;
}
else
PageControl1.Visibility = Visibility.Collapsed;
}
#region 分页控件
#region 分页属性
int pageQty = 0;
/// <summary>
/// 总页数(必须参数)
/// </summary>
public int PageQty
{
get
{
return pageQty;
}
set
{
pageQty = value;
}
}
private int lastPageNO = 10;
/// <summary>
/// 当前分页导航最后一页数值,默认值为:10
/// (注:对该属性赋值前要求先对PageQty属性进行赋值)
/// </summary>
public int LastPageNO
{
get
{
return lastPageNO;
}
set
{
if (this.PageQty == 0)
throw (new Exception("必须先定义最大页数PageQty属性!"));
lastPageNO = value > 0 ? value : 1;
}
}
int intCurrentPageNO = 1;
/// <summary>
/// 当前页号(必须参数)
/// </summary>
public int CurrentPageNO
{
get
{
try
{
return intCurrentPageNO;
}
catch
{
return 1;
}
}
set
{
if (this.pageQty > 0)
{
this.SetShow(this.LastPageNO, value);
this.PageControl1.Visibility = Visibility.Visible;
}
else
{
this.PageControl1.Visibility = Visibility.Collapsed;
}
intCurrentPageNO = value;
}
}
private string mTargetControlID = string.Empty;
public String TargetControlID
{
get
{
return mTargetControlID;
}
set
{
mTargetControlID = value;
}
}
#endregion
#region 设置导航按钮显示逻辑
/// <summary>
/// 设置导航按钮显示逻辑
/// </summary>
/// <param name="linksCount">要显示的导航数字数</param>
/// <param name="currentNO">当前页号</param>
public void SetShow(int linksCount, int currentNO)
{
if (linksCount > 10 || linksCount < 5)
{
linksCount = 10;
}
if (linksCount > this.PageQty)
{
linksCount = this.PageQty;
}
for (int i = 10; (i - linksCount) > 0; i--)
{
((Button)this.FindName(string.Format("Btn_{0}", i))).Visibility = Visibility.Collapsed;
}
for (int i = 1; i <= linksCount; i++)
{
((Button)this.FindName(string.Format("Btn_{0}", i))).Visibility = Visibility.Visible;
}
#region 设置导航数字逻辑
if ((currentNO + 4) <= linksCount)//currentNO + 4 在 1 到 linksCount-4 之间
{
setNumberLogic(1, this.CurrentPageNO, linksCount);
}
else if ((currentNO + 4) > linksCount && (currentNO + 4) <= this.PageQty) // currentNO + 4 在 linksCount-4 到 PageQty 之间
{
setNumberLogic(currentNO - (linksCount - 5), this.CurrentPageNO, linksCount);
}
else //currentNO + 4 > PageQty
{
setNumberLogic(PageQty - linksCount + 1, this.CurrentPageNO, linksCount);
}
#endregion
#region 设置前后导航
if (currentNO == 1 && currentNO != PageQty)
{
//第一页 && 不是最后一页
this.Btn_Head.IsEnabled = false;
this.Btn_Previous.IsEnabled = false;
this.Btn_Next.IsEnabled = true;
this.Btn_End.IsEnabled = true;
this.Btn_Next.Tag = (currentNO + 1).ToString();
}
else if (currentNO != 1 && currentNO == PageQty)
{
//不是第一页 && 最后一页
this.Btn_Head.IsEnabled = true;
this.Btn_Previous.IsEnabled = true;
this.Btn_Next.IsEnabled = false;
this.Btn_End.IsEnabled = false;
this.Btn_Previous.Tag = (currentNO - 1).ToString();
}
else if (currentNO != 1 && currentNO != PageQty)
{
//不是第一页 && 不是最后一页
this.Btn_Head.IsEnabled = true;
this.Btn_Previous.IsEnabled = true;
this.Btn_Next.IsEnabled = true;
this.Btn_End.IsEnabled = true;
this.Btn_Next.Tag = (currentNO + 1).ToString();
this.Btn_Previous.Tag = (currentNO - 1).ToString();
}
else
{
this.Btn_Head.IsEnabled = false;
this.Btn_Previous.IsEnabled = false;
this.Btn_Next.IsEnabled = false;
this.Btn_End.IsEnabled = false;
}
#endregion
}
/// <summary>
/// 设置导航数字逻辑
/// </summary>
/// <param name="fromNO">显示的起始页码</param>
/// <param name="currentNO">当前页码</param>
/// <param name="linksCount">显示页码数量</param>
private void setNumberLogic(int fromNO, int currentNO, int linksCount)
{
for (int i = 1; i <= linksCount; i++)
{
string btn = string.Format("Btn_{0}", i.ToString());
Button linkBtn = (Button)this.FindName(btn);
if ((fromNO + i - 1) == currentNO)
{
linkBtn.Content = currentNO.ToString();
linkBtn.IsEnabled = false;
}
else
{
string txt = (fromNO + i - 1).ToString();
linkBtn.Content = string.Format("{0}", txt);
linkBtn.Tag = txt;
linkBtn.IsEnabled = true;
}
}
}
#endregion
#region 事件
private void Button_Click(object sender, RoutedEventArgs e)
{
Button bt = sender as Button;
int messageid = Convert.ToInt32(bt.Tag);
if (messageid > 0)
{
//按钮跳转
this.CurrentPageNO = messageid;
NumberCount.Text = CurrentPageNO.ToString();
}
else
{
//用户输入页码跳转
try
{
int p = int.Parse(this.TBText.Text.Trim());
if (p > 0)
{
this.CurrentPageNO = p;
NumberCount.Text = CurrentPageNO.ToString();
}
else
return;
}
catch
{
return;
}
}
ContentDataBind();
}
#endregion
//分页控件的使用方法
public void ContentDataBind()
{
List<student> students = SetStudentInfo();
ContentGrid.Focus();
this.ContentGrid.ItemsSource = null;
this.ContentGrid.ItemsSource = (from st in students select st).Skip(PageSize * (CurrentPageNO - 1)).Take(PageSize).ToList();
var Count = students.Count();
this.ContentGrid.UpdateLayout();
PageQty = (int)Math.Ceiling((Count + 0.0) / PageSize);
if (pageQty == 0)
{
CurrentPageNO = 1;
}
else
CurrentPageNO = CurrentPageNO > PageQty ? PageQty : CurrentPageNO;
Btn_End.Tag = PageQty;
//页数大于1时才显示分页控件 可以根据自己的需要
if (PageQty > 1)
{
PageControl1.Visibility = Visibility.Visible;
}
else
PageControl1.Visibility = Visibility.Collapsed;
}