silverlight分页

一个web的分页控件改成了silverlight分页控件,现与大家分享一下 因为本例子是继上之前随笔的Deom所以没有与数据库连结,分页只能在内存中分页   在项目中此控件与row_number()存储过程分页一起使用先看一下样式

2009071913464430.jpg

使用分页后:

2009071913470487.jpg

ComboBox与RadioButton在DataGrid应用操作数据库时会像

2009071913532027.jpg

当选中一项时再按分页时会出现选中行的位置在另一页中出现空白行 解决办法:在数据绑定时让控件重新获取焦点分页控件的作用方法 如图:

  2009071914125162.gif

控件源码:

xaml:

ContractedBlock.gif ExpandedBlockStart.gif 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>

 

cs.

ContractedBlock.gif ExpandedBlockStart.gif 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;
        }

 

 

转载于:https://www.cnblogs.com/kaixun001/archive/2009/07/19/1526535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值