【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:this

 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:spa

 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.cscode

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);
        }
    }
相關文章
相關標籤/搜索