WPF+ListView+Linq+MVVM模式实现分页

http://www.cnblogs.com/tongly/archive/2010/11/10/1873881.html

1.例子实现了动态页数,当前页、总页数的显示,功能相对简单。

【效果图】

【前台界面】

复制代码
< Window  x:Class ="LinqPager.Window1"
        xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
        ResizeMode
="NoResize"
        Title
="Linq分页例子"
        WindowState
="Maximized"
        
>

    
< Grid >

        
< Grid.RowDefinitions >
            
< RowDefinition  Height ="*" />
            
< RowDefinition  Height ="30" />
        
</ Grid.RowDefinitions >

        
< ListView  Grid.Row ="0"  ItemsSource =" {Binding Lst_bind} " >
            
< ListView.View >
                
< GridView >
                    
< GridView.Columns >
                        
< GridViewColumn  DisplayMemberBinding =" {Binding Name} "  Header ="名称"     Width ="200" />
                        
< GridViewColumn  DisplayMemberBinding =" {Binding Age} "  Header ="年龄"      Width ="200" />
                        
< GridViewColumn  DisplayMemberBinding =" {Binding Address} "  Header ="地址"  Width ="200" />
                    
</ GridView.Columns >
                
</ GridView >
            
</ ListView.View >
        
</ ListView >


        
< StackPanel  Orientation ="Horizontal"  Grid.Row ="1" >

            
< ItemsControl  ItemsSource =" {Binding Pages} " >

                
< ItemsControl.ItemTemplate >
                    
< DataTemplate >
                        
< Button  Content =" {Binding Name} "  Margin ="5"  Foreground ="White"  Background ="Black"  Width ="25"  VerticalAlignment ="Center"  Click ="Button_Click" />
                    
</ DataTemplate >
                
</ ItemsControl.ItemTemplate >

                
< ItemsControl.ItemsPanel >
                    
< ItemsPanelTemplate >
                        
< WrapPanel />
                    
</ ItemsPanelTemplate >
                
</ ItemsControl.ItemsPanel >

            
</ ItemsControl >

            
< TextBlock  VerticalAlignment ="Center" >
                
                
< TextBlock  Text ="【共" />
                
< TextBlock  Text =" {Binding Total} "  Foreground ="Red" />
                
< TextBlock  Text ="页】" />
                
                
< TextBlock  Text ="【当前" />
                
< TextBlock  Text =" {Binding Currentsize} "  Foreground ="Red" />
                
< TextBlock  Text ="页】" />
                
            
</ TextBlock >

        
</ StackPanel >

    
</ Grid >
</ Window >
复制代码

 

【后台页面】

 

复制代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Data;
using  System.Windows.Documents;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Imaging;
using  System.Windows.Navigation;
using  System.Windows.Shapes;

namespace  LinqPager
{
    
///   <summary>
    
///  标识:【BruceT】
    
///  日期:【2010.11.10】
    
///   </summary>
     public   partial   class  Window1 : Window
    {

        ViewMode viewmode 
=   new  ViewMode();

        
public  Window1()
        {
            InitializeComponent();

            
this .DataContext  =  viewmode;
        }

        
private   void  Button_Click( object  sender, RoutedEventArgs e)
        {
            viewmode.Fun_Pager(((sender 
as  Button).DataContext  as  Page).PageSize);
        }
    }
  
}
复制代码

 

【ViewMode】

 

复制代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ComponentModel;

namespace  LinqPager
{
    
///   <summary>
    
///  标识:【BruceT】
    
///  日期:【2010.11.10】
    
///  描述:【ViewMode】
    
///   </summary>
     public   class  ViewMode : INotifyPropertyChanged
    {
        
private   int  _number;

        
public   int  Number
        {
            
get  {  return  _number; }
            
set  { _number  =  value; NotifyPropertyChanged( " Number " ); }
        }

        
private   int  _currentsize;

        
public   int  Currentsize
        {
            
get  {  return  _currentsize; }
            
set  { _currentsize  =  value; NotifyPropertyChanged( " Currentsize " ); }
        }

        
private   int  _total;

        
public   int  Total
        {
            
get  {  return  _total; }
            
set  { _total  =  value; NotifyPropertyChanged( " Total " ); }
        }

        
private  List < Page >  _pages;

        
public  List < Page >  Pages
        {
            
get  {  return  _pages; }
            
set  { _pages  =  value; NotifyPropertyChanged( " Pages " ); }
        }

        
private  List < User >  _lst_user;

        
public  List < User >  Lst_user
        {
            
get  {  return  _lst_user; }
            
set  { _lst_user  =  value; NotifyPropertyChanged( " Lst_user " ); }
        }

        
private  List < User >  _lst_bind;

        
public  List < User >  Lst_bind
        {
            
get  {  return  _lst_bind; }
            
set  { _lst_bind  =  value; NotifyPropertyChanged( " Lst_bind " ); }
        }


        
#region  INotifyPropertyChanged 成员

        
public   event  PropertyChangedEventHandler PropertyChanged;
        
public   void  NotifyPropertyChanged( string  PropertyName)
        {
            
if  (PropertyChanged  !=   null )
            {
                PropertyChanged(
this new  PropertyChangedEventArgs(PropertyName));
            }
        }
        
#endregion


        
public  ViewMode()
        {
            
this .Number  =   50 // 设置每页显示数目


            
this .Lst_user  =   new  List < User > (); // 初始化数据
             for  ( int  i  =   0 ; i  <   1000 ; i ++ )
            {
                Lst_user.Add(
new  User { Name  =   " A "   +  i.ToString(), Age  =  i, Address  =   " 哈尔滨 "   +  i });
            }


            
this .Total  =  Lst_user.Count()  /   this .Number  +   1 // 获得总页数,这块算法有些问题 这块应该加判断,这块可以价格取余判断。

            
this .Pages  =   new  List < Page > ();  // 初始所有页数数组
             for  ( int  i  =   1 ; i  <=   this .Total; i ++ )
            {
                
this .Pages.Add( new  Page { Name  =  i.ToString(), PageSize  =  i });
            }

            
this .Currentsize  =   1 ; // 设置当前显示页面

            Fun_Pager(
this .Currentsize);

        }

        
///   <summary>
        
///  分页方法
        
///   </summary>
        
///   <param name="PageSize"></param>
         public   void  Fun_Pager( int  Currentsize)
        {
            
this .Currentsize  =  Currentsize;
            
this .Lst_bind  =   this .Lst_user.Take( this .Number  *   this .Currentsize).Skip( this .Number  *  ( this .Currentsize  -   1 )).ToList();

        }
    }

    
public   class  User
    {
        
public   string  Name 
        { 
get set ; }
        
public   int  Age 
        { 
get set ; }
        
public   string  Address 
        { 
get set ; }
    }

    
public   class  Page
    {
        
public   string  Name 
        { 
get set ; }
        
public   int  PageSize 
        { 
get set ; }
    }
}
复制代码
  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页