WPF实现ListView的隔行换色和分页功能

用到一个工具类,PageInfo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListView翻页测试
{
    public class PageInfo<T>
    {
        public List<T> dataSource;
        int pageSize;
        int pagecount;
        int currentIndex;

        public PageInfo()
        {
            currentIndex = 1;
        }

        public PageInfo(List<T> dataSource, int pageSize)
            : this()
        {
            this.dataSource = dataSource;
            this.pageSize = pageSize;
            this.pagecount = dataSource.Count / pageSize;
            this.pagecount += (dataSource.Count % pageSize) != 0 ? 1 : 0;
        }

        public List<T> GetPageData(JumpOperation jo)
        {
            switch (jo)
            {
                case JumpOperation.GoHome:
                    currentIndex = 1;
                    break;
                case JumpOperation.GoPrePrevious:
                    if (currentIndex > 1)
                    {
                        currentIndex -= 1;
                    }
                    break;
                case JumpOperation.GoNext:
                    if (currentIndex < pagecount)
                    {
                        currentIndex += 1;
                    }
                    break;
                case JumpOperation.GoEnd:
                    currentIndex = pagecount;
                    break;
            }
            List<T> listPageData = new List<T>();
            try
            {
                int pageCountTo = pageSize;
                if (pagecount == currentIndex && dataSource.Count % pageSize > 0)
                {
                    pageCountTo = dataSource.Count % pageSize;
                }
                if (null != dataSource)
                {
                    for (int i = 0; i < pageCountTo; i++)
                    {
                        if ((currentIndex - 1) * pageSize + i < dataSource.Count)
                        {
                            listPageData.Add(dataSource[(currentIndex - 1) * pageSize + i]);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                return listPageData;
            }
            catch
            {
                return listPageData;
            }
        }
    }
    public enum JumpOperation
    {
        GoHome = 0,
        GoPrePrevious = 1,
        GoNext = 2,
        GoEnd = 3
    }
}

前端页面

<Window x:Class="ListViewSplitPages.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="525" Width="525" Loaded="Window_Loaded_1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="8*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
        <ListView AlternationCount="2" Grid.Row="0" x:Name="ListView_Students">
            <ListView.View>
                <GridView x:Name="GrdView_Students">
                    <GridViewColumn Width="100" Header="学号" DisplayMemberBinding="{Binding Path=ID}"/>
                    <GridViewColumn Width="100" Header="姓名" DisplayMemberBinding="{Binding Path=Name}"/>
                    <GridViewColumn Width="100" Header="班级" DisplayMemberBinding="{Binding Path=Class}"/>
                    <GridViewColumn Width="100" Header="年龄" DisplayMemberBinding="{Binding Path=Age}"/>
                </GridView>
            </ListView.View>
            
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Background" Value="#FF86F65B"/>
                    <Style.Triggers>
                        <Trigger Property="ListBox.AlternationIndex" Value="1">
                            <Setter Property="Background" Value="#FF39DCC2"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Border x:Name="back" Height="20">
                        <TextBlock Text="{Binding ShowText}"/>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        
</ListView>
        <Grid Grid.Row="1" Height="20">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="Button_Home" Content="首页" Grid.Column="0" Width="100" Click="Button_Home_Click"/>
            <Button x:Name="Button_Previous" Content="前一页" Grid.Column="1" Width="100" Click="Button_Previous_Click"/>
            <Button x:Name="Button_Next"  Content="下一页" Grid.Column="2" Width="100" Click="Button_Next_Click"/>
            <Button x:Name="Button_End" Content="尾页" Grid.Column="3" Width="100" Click="Button_End_Click"/>
        </Grid>
    </Grid>
</Window>

后端逻辑

using ListView翻页测试;
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 ListViewSplitPages
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Student> studentList;
        PageInfo<Student> pageStudentList;
        public MainWindow()
        {
            InitializeComponent();
            studentList = new List<Student>() {
                new Student(){ID="01",Name ="zsd",Class="1",Age="17"},
                new Student(){ID="02",Name ="fds",Class="3",Age="15"},
                new Student(){ID="03",Name ="ds",Class="1",Age="17"},
                new Student(){ID="04",Name ="fd",Class="3",Age="16"},
                new Student(){ID="05",Name ="rw",Class="1",Age="17"},
                new Student(){ID="06",Name ="hg",Class="1",Age="15"},
                new Student(){ID="07",Name ="dfd",Class="2",Age="16"},
                new Student(){ID="08",Name ="dfd",Class="2",Age="17"},
                new Student(){ID="09",Name ="gdf",Class="2",Age="17"},
                new Student(){ID="010",Name ="jh",Class="1",Age="15"},
                new Student(){ID="011",Name ="fdd",Class="2",Age="17"},
                new Student(){ID="012",Name ="rer",Class="1",Age="17"},
                new Student(){ID="013",Name ="vvv",Class="1",Age="15"},
                new Student(){ID="014",Name ="qww",Class="3",Age="16"},
                new Student(){ID="015",Name ="sds",Class="3",Age="18"},
                new Student(){ID="016",Name ="lk",Class="1",Age="17"},
                new Student(){ID="017",Name ="ui",Class="2",Age="16"},
                new Student(){ID="018",Name ="jkj",Class="1",Age="17"},
                new Student(){ID="019",Name ="zsd",Class="1",Age="17"},
                new Student(){ID="020",Name ="fds",Class="3",Age="15"},
                new Student(){ID="021",Name ="ds",Class="1",Age="17"},
                new Student(){ID="022",Name ="fd",Class="3",Age="16"},
                new Student(){ID="023",Name ="rw",Class="1",Age="17"},
                new Student(){ID="024",Name ="hg",Class="1",Age="15"},
                new Student(){ID="025",Name ="dfd",Class="2",Age="16"},
                new Student(){ID="026",Name ="dfd",Class="2",Age="17"},
                new Student(){ID="027",Name ="gdf",Class="2",Age="17"},
                new Student(){ID="028",Name ="jh",Class="1",Age="15"},
                new Student(){ID="029",Name ="fdd",Class="2",Age="17"},
                new Student(){ID="030",Name ="rer",Class="1",Age="17"},
                new Student(){ID="031",Name ="vvv",Class="1",Age="15"},
                new Student(){ID="032",Name ="qww",Class="3",Age="16"},
                new Student(){ID="033",Name ="sds",Class="3",Age="18"},
                new Student(){ID="034",Name ="lk",Class="1",Age="17"},
                new Student(){ID="035",Name ="ui",Class="2",Age="16"},
                new Student(){ID="036",Name ="jkj",Class="1",Age="17"},

            };
            pageStudentList = new PageInfo<Student>(studentList, 16);
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoHome);
        }

        private void Button_Home_Click(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoHome);
        }

        private void Button_Previous_Click(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoPrePrevious);
        }

        private void Button_Next_Click(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoNext);
        }

        private void Button_End_Click(object sender, RoutedEventArgs e)
        {
            this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoEnd);
        }
    }

    public class Student
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
        public string Age { get; set; }
    }
}

效果图
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值