首页 > 代码库 > wpf listbox分页

wpf listbox分页

        List<ListBoxItem> llb;//符合条件像的listbox数据集合
      public static List<ModelDataBase> datalist = new List<ModelDataBase>();//总的数据源
      int pageIndex = 1;//当前页
      int pageSize = 16;//一页最大容量
      int totalPage = 0;//总页数

      public void TakePage()
        {
            tbindex.Text = pageIndex.ToString();
            listBox.ItemsSource = llb.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

//首页
      private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            pageIndex = 1;
            TakePage();
        }

        //尾页
      private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            pageIndex = totalPage;
            TakePage();
        }

        //上一页
      private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex -= 1; TakePage();
            }
        }

        //下一页
      private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (pageIndex < totalPage)
            {
                pageIndex += 1;
                TakePage();
            }
        }

public List<ListBoxItem> BoundData(int _pageIndex, int _pageSize, string strString,string sign)
        {
            llb = new List<ListBoxItem>();
            List<ModelDataBase> listmodel = null;
            if (sign.Equals("1"))
            {
                listmodel = datalist.FindAll(item => (int)item._ParentID == Convert.ToInt64(strString));
            }
            else if (sign.Equals("0"))
            {
                listmodel = datalist.Where(s => s._ModelName.Contains(strString)).ToList();
            }
            else
            {
                listmodel = datalist.Where(s => s._ModelName == strString).ToList();
            }
            foreach (ModelDataBase item in listmodel)
            {
                ListBoxItem lbi = new ListBoxItem();
                lbi.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                StackPanel sp = new StackPanel();
                Image img = new Image();
                string url = item._ModelURL;
                img.Source = new BitmapImage(new Uri(@url, UriKind.Relative));
                System.Windows.Controls.Label lable = new System.Windows.Controls.Label();
                lable.Content = item._ModelName;
                sp.Children.Add(img);
                sp.Children.Add(lable);
                sp.Tag = item._MidelRemark;
                lbi.Content = sp;
                lbi.MouseDoubleClick += Lbi_MouseDoubleClick;
                llb.Add(lbi);
            }
            //初始化分页元素
            pageIndex = 1;
            tbindex.Text = pageIndex.ToString();
            int count = listmodel.Count;//总数据条数
            if (count % pageSize == 0)
            {
                totalPage = count / pageSize;
            }
            else
            {
                totalPage = count / pageSize + 1;
            }
            tbmax.Text = totalPage.ToString();
            DisableClick(true);
            return llb.Skip(0).Take(pageSize).ToList();
        }

 

wpf listbox分页