WPF Listview 排序加分页

RadioButton radio = sender as RadioButton;
            courseHoursList.Items.SortDescriptions.Clear();
            if (radio.Name == "sort_asc")
            {
                courseHoursList.Items.SortDescriptions.Add(new SortDescription("create_time", ListSortDirection.Ascending));
            }
            else
            {
                courseHoursList.Items.SortDescriptions.Add(new SortDescription("create_time", ListSortDirection.Descending));
            }

  

分页

#region  分页

        private void but_Pages_Click(object sender, RoutedEventArgs e)
        {
            Fun_Pager(((sender as Button).DataContext as Page).PageSize);
        }


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

        private int _currentSize;
        public int CurrentSize
        {
            get { return _currentSize; }
            set
            {
                _currentSize = value;
                OnPropertyChanged("CurrentSize");
            }
        }

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

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

        private List<LessonInfo> _listRegDept;
        public List<LessonInfo> ListRegDept
        {
            get { return _listRegDept; }
            set
            {
                _listRegDept = value;
                OnPropertyChanged("ListRegDept");
            }
        }

        private List<LessonInfo> _listBind;
        public List<LessonInfo> ListBind
        {
            get { return _listBind; }
            set
            {
                _listBind = value;
                OnPropertyChanged("ListBind");
            }
        }

        //初始化,传list
        public void ViewPages(List<LessonInfo> lessonInfos)
        {
            LessonInfo dataOP = new LessonInfo();
            this.Number = 1;//设置每页显示数目
            this.ListRegDept = new List<LessonInfo>();//初始化数据
            ListRegDept = lessonInfos;

            if ((ListRegDept.Count() % this.Number) > 0)
            {
                this.Total = ListRegDept.Count() / this.Number + 1;//获取总页数
            }
            else
            {
                this.Total = ListRegDept.Count() / this.Number;//获取总页数
            }

            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="CurrentSize">当前页数</param>
        public void Fun_Pager(int CurrentSize)
        {
            this.CurrentSize = CurrentSize;
            this.ListBind = this.ListRegDept.Take(this.Number * this.CurrentSize)
                .Skip(this.Number * (this.CurrentSize - 1)).ToList();

            dataList.ItemsSource = this.ListBind;
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            Fun_Pager(1);
        }

        private void btnEnd_Click(object sender, RoutedEventArgs e)
        {
            Fun_Pager(this.Pages.Count);
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentSize > 1)
            {
                Fun_Pager(CurrentSize - 1);
            }
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentSize < Pages.Count)
            {
                Fun_Pager(CurrentSize + 1);
            }
        }
    }

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

    #endregion

  添加在listview 下面

<StackPanel Grid.Row="4"  Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
            <StackPanel Orientation="Horizontal">
                <Button Content="首页" Style="{DynamicResource PageNumButton}" Click="btnHome_Click" Margin="0,0,10,0" Foreground="#393838" Background="White" FontSize="12" Width="40" Height="22" Cursor="Hand"/>
                <!--<ItemsControl ItemsSource="{Binding Pages}" HorizontalAlignment="Center">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Button Name="but_Pages" Content="{Binding Name}" Margin="0,0,10,0" Style="{DynamicResource PageNumButton}" Foreground="#393838" FontSize="12" Background="White"  Click="but_Pages_Click" Width="30" Height="22" Cursor="Hand"></Button>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>-->
                <Button Content="上一页" Style="{DynamicResource PageNumButton}" Click="btnLast_Click" Margin="0,0,10,0" Foreground="#393838" Background="White" FontSize="12" Width="40" Height="22" Cursor="Hand"/>
                <Button Content="下一页" Style="{DynamicResource PageNumButton}" Click="btnNext_Click" Margin="0,0,10,0" Foreground="#393838" Background="White" FontSize="12" Width="40" Height="22" Cursor="Hand"/>
                <Button Content="尾页" Style="{DynamicResource PageNumButton}" Click="btnEnd_Click"  Foreground="#393838" Background="White" FontSize="12" Width="40" Height="22" Cursor="Hand"/>
            </StackPanel>
            <TextBlock VerticalAlignment="Center" Margin="20,0,0,0" FontSize="10">
                <TextBlock Text="【共"/>
                <TextBlock Text="{Binding Total}" Foreground="#329FEE"/>
                <TextBlock Text="页】"/>
                <TextBlock Text="【当前第"/>
                <TextBlock Text="{Binding CurrentSize}" Foreground="#329FEE" />
                <TextBlock Text="页】"/>
            </TextBlock>
        </StackPanel>

 

posted @ 2020-04-16 16:41  寒夜美美  阅读(405)  评论(0编辑  收藏  举报