微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight使用DataPager实现DataGrid分页

1)前台代码

<UserControl x:Class="SilverlightApplication70.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">


    <Grid x:Name="LayoutRoot" Background="White">
        <!--用StackPanel解决DataGrid和DataPager绑定之后,DataGrid行无法选中。-->
        <StackPanel Orientation="Vertical">
        <data:DataGrid Name="TestDataGrid" AutoGenerateColumns="False">
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="StudentID" Binding="{Binding StudentID}">
                </data:DataGridTextColumn>
                <data:DataGridTextColumn Header="StudentName" Binding="{Binding StudentName}">
                </data:DataGridTextColumn>
            </data:DataGrid.Columns>
        </data:DataGrid>
        <data:DataPager Name="TestDataPager"/>
        </StackPanel>
    </Grid>
</UserControl>

2)后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace SilverlightApplication70
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }


        void MainPage_Loaded(object sender,RoutedEventArgs e)
        {
            List<Student> items=GetStudents();
            System.Windows.Data.PagedCollectionView pagedCollectionView = new System.Windows.Data.PagedCollectionView(items);
            pagedCollectionView.PageSize = 5;
            TestDataGrid.ItemsSource = pagedCollectionView;
            TestDataPager.source = pagedCollectionView;
        }
        /// <summary>
        /// 获取数据源
        /// </summary>
        /// <returns></returns>
        private List<Student> GetStudents()
        {
            List<Student> items = new List<Student>();
            for (int i = 0; i < 20; i++)
            { 
                items.Add(new Student(i,"张三"+i));
            }
            return items;
        }
    }


    public class Student
    {
        public Student(){ }
        public Student(int _studentID,string _studentName)
        {
            this.StudentID = _studentID;
            this.StudentName = _studentName;
        }
        private int studentID;


        public int StudentID
        {
            get { return studentID; }
            set { studentID = value; }
        }
        private string studentName;


        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }
    }
}


3)个人小结

silverlight使用DataPager实现DataGrid分页是客户端分页,在客户端选择上一页下一页时是不会连接数据库的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐