在SilverLight 中,我们同样可以想.Net中那样读取数据并显示在页面上,但是,在Silverlight中,我们就不用GridView,而是使用DataGrid来实现同样的效果,好了,废话不多说,且看如何实现吧
1、首先来建立一个测试页面DataPager.xaml,其中代码如下:
- <navigation:Page x:Class="MySilverLight.DataPager"
- 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"
- xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
- mc:Ignorable="d"
- xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
- d:DesignWidth="640" d:DesignHeight="480"
- Title="DataPager Page">
- <Grid x:Name="LayoutRoot">
- <StackPanel>
- <StackPanel Margin="10">
- <data:DataPager x:Name="dp_top"
- PageSize="5"
- IsTotalItemCountFixed="True"
- NumericButtonCount="10"
- DisplayMode="FirstLastPreviousNext"
- AutoEllipsis="True">
- </data:DataPager>
- <data:DataGrid x:Name="gdv_data"
- />
- <data:DataPager x:Name="dp_footer"
- PageSize="6" NumericButtonCount="10" AutoEllipsis="True"
- DisplayMode="FirstLastPreviousNextNumeric"
- IsTotalItemCountFixed="True">
- </data:DataPager>
- </StackPanel>
- </StackPanel>
- </Grid>
- </navigation:Page>
值得注意的是:在头部需要加入xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
并且需要引入如下的命名空间 System.Windows.Controls和System.Windows.Controls.Data
2、接下来我们需要准备一些数据,后面会用到
先来建立一个Users.cs类,代码如下:
- namespace MySilverLight
- {
- public class Users
- {
- private int _id;
- private string _name;
- private int _age;
- private string _address;
- public int Id
- {
- get { return _id; }
- set { _id = value; }
- }
- public string Name
- {
- get { return _name; }
- set { _name = value; }
- }
- public int Age
- {
- get { return _age; }
- set { _age = value; }
- }
- public string Address
- {
- get { return _address; }
- set { _address = value; }
- }
- }
- }
然后在DataPager.xaml.cs文件中写一个方法,向里面添加数据,代码如下:
- List<Users> Data()
- {
- List<Users> list = new List<Users>() {
- new Users{Id=0001,Name="黄晓华",Age=30,Address="河南 贵州"},
- new Users{Id=0002,Name="陈东",Age=20,Address="四川 达州"},
- new Users{Id=0003,Name="李丽",Age=22,Address="四川 成都"},
- new Users{Id=0004,Name="王武",Age=41,Address="四川 遂宁"},
- new Users{Id=0005,Name="李华",Age=34,Address="四川 南充"},
- new Users{Id=0006,Name="文琪",Age=23,Address="重庆 大川"},
- new Users{Id=0007,Name="消炎",Age=25,Address="江西 南阳"},
- new Users{Id=0008,Name="罗翰",Age=15,Address="四川 绵阳"},
- new Users{Id=0009,Name="张丹",Age=23,Address="四川 广汉"},
- new Users{Id=0010,Name="玉立",Age=26,Address="四川 内江"},
- new Users{Id=0011,Name="蒋晓华",Age=26,Address="四川 邛崃"},
- new Users{Id=0012,Name="郭丽",Age=76,Address="四川 广元"},
- new Users{Id=0013,Name="韩小帅",Age=34,Address="四川 广安"},
- new Users{Id=0014,Name="黄晓华",Age=30,Address="河南 贵州"},
- new Users{Id=0015,Name="陈东",Age=20,Address="四川 达州"},
- new Users{Id=0016,Name="李丽",Age=22,Address="四川 成都"},
- new Users{Id=0017,Name="王武",Age=41,Address="四川 遂宁"},
- new Users{Id=0018,Name="李华",Age=34,Address="四川 南充"},
- new Users{Id=0019,Name="文琪",Age=23,Address="重庆 大川"},
- new Users{Id=0020,Name="消炎",Age=25,Address="江西 南阳"},
- new Users{Id=0021,Name="罗翰",Age=15,Address="四川 绵阳"},
- new Users{Id=0022,Name="张丹",Age=23,Address="四川 广汉"},
- new Users{Id=0023,Name="玉立",Age=26,Address="四川 内江"},
- new Users{Id=0024,Name="蒋晓华",Age=26,Address="四川 邛崃"},
- new Users{Id=0025,Name="郭丽",Age=76,Address="四川 广元"},
- new Users{Id=0026,Name="韩小帅",Age=34,Address="四川 广安"},
- new Users{Id=0027,Name="黄晓华",Age=30,Address="河南 贵州"},
- new Users{Id=0028,Name="陈东",Age=20,Address="四川 达州"},
- new Users{Id=0029,Name="李丽",Age=22,Address="四川 成都"},
- new Users{Id=0030,Name="王武",Age=41,Address="四川 遂宁"},
- new Users{Id=0031,Name="李华",Age=34,Address="四川 南充"},
- new Users{Id=0032,Name="文琪",Age=23,Address="重庆 大川"},
- new Users{Id=0033,Name="消炎",Age=25,Address="江西 南阳"},
- new Users{Id=0034,Name="罗翰",Age=15,Address="四川 绵阳"},
- new Users{Id=0035,Name="张丹",Age=23,Address="四川 广汉"},
- new Users{Id=0036,Name="玉立",Age=26,Address="四川 内江"},
- new Users{Id=0037,Name="蒋晓华",Age=26,Address="四川 邛崃"},
- new Users{Id=0038,Name="郭丽",Age=76,Address="四川 广元"},
- new Users{Id=0039,Name="韩小帅",Age=34,Address="四川 广安"}
- };
- return list;
- }
3、最后,我们来写绑定数据的代码:
- public DataPager()
- {
- InitializeComponent();
- this.Loaded += new RoutedEventHandler(DataPager_Loaded);
- }
- void DataPager_Loaded(object sender, RoutedEventArgs e)
- {
- List<Users> list= Data();
- //绑定数据
- PagedCollectionView view = new PagedCollectionView(list);
- gdv_data.ItemsSource = view;
- dp_top.Source = view;
- dp_footer.Source = view;
- }
其中PagedCollectionView 需要引用System.Windows.Data命名空间,运行后的效果如下: