【WPF】ListView自定义分页

XAML:

 1   <Grid>
 2                     <Grid.RowDefinitions>
 3                         <RowDefinition Height="*"/>
 4                         <RowDefinition Height="30"/>
 5                     </Grid.RowDefinitions>
 6                     <ListView Grid.Row="0" ItemsSource="{Binding Lst_bind}">
 7                         <ListView.View>
 8                             <GridView>
 9                                 <GridView.Columns>
10                                     <GridViewColumn DisplayMemberBinding="{Binding Name}" 
11                                                     Width="200" Header="名字"/>
12                                     <GridViewColumn DisplayMemberBinding="{Binding Age}"
13                                                     Width="200" Header="年龄"/>
14                                     <GridViewColumn DisplayMemberBinding="{Binding Address}"
15                                                     Width="200" Header="地址"/>
16                                 </GridView.Columns>
17                             </GridView>
18                         </ListView.View>
19                     </ListView>
20                     
21                     <StackPanel Orientation="Horizontal" Grid.Row="1">
22                         <ItemsControl ItemsSource="{Binding Pages}">
23                             <ItemsControl.ItemTemplate>
24                                 <DataTemplate>
25                                     <WrapPanel>
26                                     <Button Content="{Binding Name}" Background="Red" Foreground="White"
27                                             Width="25" VerticalAlignment="Center" Click="Button_Click_1"/>
28                                     </WrapPanel>
29                                 </DataTemplate>
30                             </ItemsControl.ItemTemplate>
                               <!--这里用WrapPanel 当容器放Button-->
31 <ItemsControl.ItemsPanel> 32 <ItemsPanelTemplate> 33 <WrapPanel Orientation="Horizontal"/> 34 </ItemsPanelTemplate> 35 </ItemsControl.ItemsPanel> 36 </ItemsControl> 37 <TextBlock VerticalAlignment="Center" Foreground="Black"> 38 39 <TextBlock Text="【共"/> 40 <TextBlock Text="{Binding Total}" Foreground="Red"/> 41 <TextBlock Text="页】"/> 42 43 <TextBlock Text="【当前"/> 44 <TextBlock Text="{Binding Currentsize}" Foreground="Red"/> 45 <TextBlock Text="页】"/> 46 47 </TextBlock> 48 </StackPanel> 49 </Grid>

后台代码:
  Models:

 public class Pages
    {
        public string Name { get; set; }
        public int PageSize { get; set; }
    }

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

ViewMode:

 class PageDataManager: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<Pages> pages;
        public List<Pages> 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"); }
        }

        //负责监视属性的变化
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string Propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(Propertyname));
            }
        }

        public PageDataManager()
        {
            this.Number = 50;

            this.Lst_user = new List<User>();
            for (int i = 0; i <= 1000; i++)
            {
                Lst_user.Add(new User() { Name="张三"+i.ToString(), Age=20, Address="中国河南" });
            }

            //总页数=总数/每页显示的数
            this.Total = Lst_user.Count()/Number;

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


        //分页方法
        public void Pager(int cize)
        {
            this.Currentsize = cize;
            this.Lst_bind = this.Lst_user.Take(this.Number * cize).Skip(this.Number * (cize - 1)).ToList();
        }
    }

MainPage.cs

public partial class MainWindow : Window
    {
        PageDataManager data = new PageDataManager();
        public MainWindow()
        {
          
            InitializeComponent();
            this.DataContext = data;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            data.Pager(((sender as Button).DataContext as Pages).PageSize);
        }
    }
原文地址:https://www.cnblogs.com/wywnet/p/3495525.html