Silverlight强大的DataGrid组件_第1页
Silverlight强大的DataGrid组件_第2页
Silverlight强大的DataGrid组件_第3页
Silverlight强大的DataGrid组件_第4页
Silverlight强大的DataGrid组件_第5页
已阅读5页,还剩117页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.Silverlight强大的DataGrid组件1说明:DataGrid组件是Silverlight数据组件中最为常用并且是功能最为强大的数据组件。因此,对开发者而言,深入了解其特性是十分有必要的。本文先介绍该组件的基本特性,接着通过几个简单实例来说明该组件的基本数据操作过程。组件所在命名空间:System.Windows.Controls组件常用方法:BeginEdit:使DataGrid进入编辑状态。CancelEdit:取消DataGrid的编辑状态。CollapseRowGroup:闭合DataGrid的行分组。CommitEdit:确认DataGrid的编辑完成。ExpandRow

2、Group:展开DataGrid的行分组。GetGroupFromItem:从具体Item中得到分组。ScrollIntoView:滚动DataGrid视图。组件常用属性:AlternatingRowBackground:获取或设置一个笔刷用来描绘DataGrid奇数行的背景。AreRowDetailsFrozen:获取或设置一个值用来判断是否冻结每行内容的详细信息。AreRowGroupHeadersFrozen:获取或设置一个值用来判断是否冻结分组行的头部。AutoGenerateColumns:获取或设置一个值用来判断是否允许自动生成表列。CanUserReorderColumns:获取

3、或设置一个值用来判断是否允许用户重新排列表列的位置。CanUserSortColumns:获取或设置一个值用来判断是否允许用户按列对表中内容进行排序。CellStyle:获取或设置单元格的样式。ColumnHeaderHeight:获取或设置列头的高度。ColumnHeaderStyle:获取或设置列头的样式。Columns:获取组件中包含所有列的集合。ColumnWidth:获取或设置列宽。CurrentColumn:获取或设置包含当前单元格的列。CurrentItem:获取包含当前单元格且与行绑定的数据项。DragIndicatorStyle:获取或设置当拖曳列头时的样式。DropLoca

4、tionIndicatorStyle:获取或设置呈现列头时的样式。FrozenColumnCount:获取或设置冻结列的个数。GridLinesVisibility:获取或设置网格线的显示形式。HeadersVisibility:获取或设置行头及列头的显示形式。HorizontalGridLinesBrush:获取或设置水平网格线的笔刷。HorizontalScrollBarVisibility:获取或设置水平滚动条的显示样式。IsReadOnly:获取或设置DataGrid是否为只读。MaxColumnWidth:获取或设置DataGrid的最大列宽。MinColumnWidth:获取或设置

5、DataGrid的最小列宽。RowBackground:获取或设置用于填充行背景的笔刷。RowDetailsTemplate:获取或设置被用于显示行详细部分的内容的模板。RowDetailsVisibilityMode:获取或设置一个值用以判定行详细部分是否显示。RowGroupHeaderStyles:获取呈现行分组头部的样式。RowHeaderStyle:获取或设置呈现行头的样式。RowHeaderWidth:获取或设置行头的宽度。RowHeight:获取或设置每行的高度。RowStyle:获取或设置呈现行时的样式。SelectedIndex:获取或设置当前选中部分的索引值。Selecte

6、dItem:获取或设置与当前被选中行绑定的数据项。SelectedItems:获取与当前被选中的各行绑定的数据项们的列表(List)。SelectionMode:获取或设置DataGrid的选取模式。VerticalGridLinesBrush:获取或设置垂直网格线的笔刷。VerticalScrollBarVisibility:获取或设置垂直滚动条的显示样式。组件常用事件:BeginningEdit:发生于一个单元格或行进入编辑模式之前。CellEditEnded:发生于一个单元格编辑已被确认或取消。CellEditEnding:发生于一个单元格正在结束编辑时。CurrentCellChang

7、ed:发生于一个单元格成为当前单元格时。PreparingCellForEdit:发生于在DataGridTemplateColumn下的单元格进入编辑模式时。SelectionChanged:发生于当SelectedItem或SelectedItems属性值改变时。实例:为DataGrid提供数据源的常用类型主要有两类:List和ObservableCollection。前者一般用于普通数据绑定,而后者则常用于进行数据的双向绑定来保证数据的同步性。下面就分别给出这两种DataProvider的例子:List1)最简单的绑定效果图:MainPage.xaml文件代码:<UserContr

8、ol     xmlns="     xmlns:x="     xmlns:d=" xmlns:mc="/markup-compatibility/2006"     mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Contr

9、ols;assembly=System.Windows.Controls.Data" x:Class="SilverlightClient.MainPage"     Width="640" Height="480">     <Grid x:Name="LayoutRoot" Background="White" Width="640" Height="480&q

10、uot;>         <data:DataGrid x:Name="dgEmployee" Height="188" HorizontalAlignment="Left" Margin="18,19,0,0" VerticalAlignment="Top" Width="302"/>     </Grid> </Us

11、erControl>MainPage.xaml.cs文件代码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; us

12、ing System.Windows.Shapes;   namespace SilverlightClient .     public partial class MainPage : UserControl     .         public MainPage()         .    

13、60;        InitializeComponent();             this.Loaded += new RoutedEventHandler(MainPage_Loaded);                  &

14、#160; void MainPage_Loaded(object sender, RoutedEventArgs e)         .             string dataSource = "H e l l o !"            &#

15、160;string sp = . " " ;             dgEmployee.ItemsSource = dataSource.Split(sp, StringSplitOptions.None).ToList();               2)使用业务对象效果图:MainPage.xaml文件代码:<UserC

16、ontrol     xmlns="     xmlns:x="     xmlns:d=" xmlns:mc="/markup-compatibility/2006"     mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.C

17、ontrols;assembly=System.Windows.Controls.Data" x:Class="SilverlightClient.MainPage"     Width="640" Height="480">     <Grid x:Name="LayoutRoot" Background="White" Width="640" Height="4

18、80">         <data:DataGrid x:Name="dgEmployee" Height="188" HorizontalAlignment="Left" Margin="18,19,0,0" VerticalAlignment="Top" Width="302"/>     </Grid> <

19、;/UserControl>MainPage.xaml.cs文件代码: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.Animatio

20、n; using System.Windows.Shapes;   namespace SilverlightClient .     /定义数据类     public class Employees     .         public int EmployeeID . get; set;       

21、0; public string EmployeeName . get; set;         public int EmployeeAge . get; set;           public Employees()         .        

22、;   public Employees(int employeeid, string employeename, int employeeage)         .             EmployeeID = employeeid;           

23、60; EmployeeName = employeename;             EmployeeAge = employeeage;                     public partial class MainPage : UserControl  

24、60;  .         List em = new List();                 public MainPage()         .         &

25、#160;   InitializeComponent();             this.Loaded += new RoutedEventHandler(MainPage_Loaded);                    void MainPage_Loa

26、ded(object sender, RoutedEventArgs e)         .             em.Clear();             em.Add(new Employees(1, "张三", 23);   

27、;          em.Add(new Employees(2, "李四", 24);             em.Add(new Employees(3, "王五", 25);              

28、; dgEmployee.ItemsSource = em;               ObservableCollection注:要实现数据同步的双向绑定,则业务对象一定要实现INotifyPropertyChanged接口。效果图: MainPage.xaml文件代码:<UserControl     xmlns="     xmlns:x="

29、    xmlns:d=" xmlns:mc="/markup-compatibility/2006"     mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightClient

30、.MainPage"     Width="640" Height="480">     <Grid x:Name="LayoutRoot" Background="White" Width="640" Height="480">         <data:DataGrid x:Na

31、me="dgEmployee" Height="188" HorizontalAlignment="Left" Margin="18,19,0,0" VerticalAlignment="Top" Width="302"/>          <ListBox x:Name="lbSynchronization" HorizontalAlignment="

32、Left" Margin="18,231,0,71" Width="302"/>     </Grid> </UserControl>MainPage.xaml.cs文件代码:using .System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Net; using System.Windows; using

33、System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.ComponentModel;   namespace SilverlightClient .     public class Employees :

34、INotifyPropertyChanged     .         private string name;         public string  EmployeeName         .        

35、0;  get  . return name;           set            .             if( value != name)     

36、0;       .               name  = value;               onPropertyChanged(this, "EmployeeName");  

37、0;                                       public event PropertyChangedEventHandler PropertyChanged;    

38、60;      private void onPropertyChanged(object sender, string propertyName).           if(this.PropertyChanged != null)         .         &#

39、160;  PropertyChanged(sender,new PropertyChangedEventArgs(propertyName) ;                         public Employees()         .    

40、       public Employees(string employeename)         .             EmployeeName = employeename;             &#

41、160;       public partial class MainPage : UserControl     .         ObservableCollection getEmployeesCollection()         .        

42、60;    ObservableCollection rVal = new ObservableCollection();               rVal.Add(new Employees . EmployeeName = "Tim" );             rV

43、al.Add(new Employees . EmployeeName = "Mary" );             rVal.Add(new Employees . EmployeeName = "John" );                   &#

44、160;     return rVal;                    public ObservableCollection em;           public MainPage()      

45、0;  .             InitializeComponent();             this.Loaded += new RoutedEventHandler(MainPage_Loaded);            

46、        void MainPage_Loaded(object sender, RoutedEventArgs e)         .             em = getEmployeesCollection();        &#

47、160;    dgEmployee.ItemsSource = em;             lbSynchronization.ItemsSource = em;             lbSynchronization.DisplayMemberPath = "EmployeeName"

48、              在上一篇中,我们讨论了DataGrid的基础数据绑定的有关知识。在今天的教程中,我将为大家介绍怎样使用ADO.NET Entity Framework来与数据库进行基本的交互。概览为了能够使事情变得简单易做,这里我先为大家讲解怎样从数据库检索数据并返回至DataGrid。这个例子将为我们展示如下的功能:1)点击Get Data按钮向后台发送获取数据消息2)后台数据库将得到的相关数据传回至DataGrid。这里,如果用的测试数据的量较大的话可以观测到

49、明显的延时现象(这就是异步操作带来的效果)。简单的准备工作为了能使ADO.NET Entity Framework正常工作,请为你的VS2008打上SP1服务包。附SP1下载地址:中文版:英文版:创建项目点击菜单File->New->Project.,打开New Project对话框。在Project types中选择Web,在右侧的Templates中选择ASP.NET Web Application,输入项目名为testDataGrid,点击OK按钮,完成创建。(见图1)  图1:创建ASP.NET Web Application接着,右击Solution

50、Explorer中Solution 'testDataGrid'根文件夹,依次点击菜单选项Add->New Project.。在Project types中选择Silverlight,在右侧的Templates中选择Silverlight Application,输入项目名为SilverlightClient,点击OK按钮,完成创建。(见图2和图3)图2:新建Silverlight项目图3:添加新项目在弹出的对话框中,按下图进行设置,然后点击OK按钮。图4:新建Silverlight应用程序创建数据库为了加快实验速度,我们使用SQL Server Express 2008

51、作为后台数据库。按如下步骤建立数据库及数据表:1)右击testDataGrid文件夹下的App_Data文件夹,依次点击菜单选项Add->New Item.。在弹出的对话框中,输入数据库名为Employees.mdf,点击Add按钮。图5:创建数据库Employees双击Employees.mdf,打开Server Explorer窗口。按下两图所示添加新数据表,将该表命名为Employee。图6:添加新数据表图7:Employee表设置输入一些测试数据。图8:输入一些测试数据创建ADO.NET Entity数据库实体模型右击testDataGrid文件夹,点击菜单选项Add->N

52、ew Item.。按下图进行操作,将数据库实体命名为EmployeeModel.edmx,点击Add按钮。图9:新建实体模型在弹出对话框中选Generate from database,点击Next按钮。按下图进行操作,直到Finish为止。图10:从数据库生成模型图11:选择数据链接图12:选择数据库对象这样数据模型就建立完毕。建立ADO.NET Data Service数据通讯服务右击testDataGrid文件夹,依次点击Add->New Item.。然后按下图操作,将ADO.NET Data Service服务命名为EmployeeInfoService.svc。 图1

53、3:创建ADO.NET Data Service修改EmployeeInfoService.svc.cs代码如下:using .System; using System.Collections.Generic; using System.Data.Services; using System.Linq; using System.ServiceModel.Web; using System.Web; namespace testDataGrid . public class EmployeeInfoService : DataService . public static void Initi

54、alizeService(IDataServiceConfiguration config) . config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); 右击刚才创建的EmployeeInfoService.svc,选择菜单选项View in Browser(如下图),配置成功的话会出现下图所示的页面。按Ctrl+Shift+B进行整个项目的编译(非常重要,

55、不然数据服务引用会出错!)图14:查看Web Service配置图15:配置成功时出现的页面右击SilverlightClient项目文件夹下的References,选择Add Service References.。接着,在弹出的对话框中点击Discover按钮,Services中出现刚才我们创建的EmployeeInfoService.svc,点击其左边的“+”展开符号,之后出现服务搜寻,结束后将Namespace改为EmployeeServiceReference。(见下图)。图16:添加服务引用这样,建立ADO.NET Data Service数据通讯服务的过程就此结束。创建Silve

56、rlightClient界面及组件代码MainPage.xaml代码:<UserControl xmlns=" xmlns:x=" xmlns:d=" xmlns:mc="/markup-compatibility/2006" mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

57、x:Class="SilverlightClient.MainPage" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot" Background="White" Width="320" Height="220"> <data:DataGrid x:Name="dgEmployee" Height="150"

58、Margin="8,8,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="304"/> <Button x:Name="btnGetData" Height="28" HorizontalAlignment="Left" Margin="8,171,0,0" VerticalAlignment="Top" Width="

59、98" Content="Get Data"/> </Grid> </UserControl>MainPage.xaml.cs代码: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

60、 System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Data.Services.Client;/引入System.Data.Services.Client命名空间 using SilverlightClient.EmployeeServiceReference;/引入数据服务所在命名空间 namespace SilverlightClient . public partial class MainPage : UserControl . pu

61、blic MainPage() . InitializeComponent(); /注册事件触发处理 this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click); void btnGetData_Click(object sender, RoutedEventArgs e) . EmployeesEntities proxy = new EmployeesEntities(new Uri("EmployeeInfoService.svc", UriKind.Relative); var query =

62、from c in proxy.Employee select c; DataServiceQuery userQuery = (DataServiceQuery)query; userQuery.BeginExecute(new AsyncCallback(OnLoadComplete), query);/异步调用 void OnLoadComplete(IAsyncResult result) . DataServiceQuery query = (DataServiceQuery)result.AsyncState; dgEmployee.ItemsSource = query.EndE

63、xecute(result).ToList();/将最终结果传给DataGrid 最终效果图最后,按F5键进行编译测试。 在上一篇中,我们讨论了怎样使用ADO.NET Entity Framework+ADO.NET Data Service与数据库进行交互的例子。本教程将使用另一常见的数据模型工具Linq to SQL Class+Silverlight-enabled WCF Service来与数据库进行交互。准备工作1)测试项目的建立2)创建测试用数据库强大的DataGrid组件3_数据交互之Linq to SQL由于在上一篇中我已进行详细的介绍,这里就不在赘述。创建Linq

64、to SQL 数据模型类右击testDataGrid文件夹,点击菜单选项Add->New Item.。按下图进行操作,将数据库实体命名为EmployeeModel.dbml,点击Add按钮。图1:新建Linq to SQL数据实体模型打开Server Explorer,将Employees数据库中的Employee表拖入Linq to SQL设计器中,并且将设计器中的Employee改为Employees。(如下图)图2:设计数据模型接着,对EmployeeModel.dbml的属性进行设置。将Context Namespace设置为EmployeesContext,将Entity Na

65、mespace设置为EmployeesEntities。图3:设置EmployeeModel.dbml的属性按Ctrl+Shift+B,进行项目的编译。建立Silverlight-enabled WCF Service数据通信服务右击testDataGrid文件夹,点击菜单选项Add->New Item.。按下图进行操作,将WCF Service命名为EmployeeInfoWCFService.svc,点击Add按钮。图4:新建Silverlight-enabled WCF Service之后,将下述代码添加至EmployeeInfoWCFService.svc.cs文件中。using

66、 .System;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Activation;using System.Collections.Generic;using System.Text;using EmployeesContext;/引入数据库实体所在命名空间using EmployeesEntities;/引入数据表实体所在命名空间namespace testDataGrid.    Se

67、rviceContract(Namespace = "")    AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)    public class EmployeeInfoWCFService    .        /添加要调用的方法以

68、标签OperationContract开头        OperationContract        public List GetEmployees()        .            EmployeeModelDataCo

69、ntext db = new EmployeeModelDataContext();            return db.Employees.ToList();             按Ctrl+Shift+B,进行项目的编译。右击SilverlightClient项目文件夹下的References,选择Add Service Refere

70、nces.。接着,在弹出的对话框中点击Discover按钮,Services中出现刚才我们创建的EmployeeInfoWCFService.svc,点击其左边的“+”展开符号,之后出现服务搜寻,结束后将Namespace改为EmployeeWCFServiceReference。(见下图)。 图5:添加EmployeeInfoWCFService.svc的引用按Ctrl+Shift+B,进行项目的编译。这样,建立Silverlight-enabled WCF Service数据通讯服务的过程就此结束。创建SilverlightClient界面及组件代码MainPage.xaml代码

71、: <UserControl    xmlns="     xmlns:x="    xmlns:d="    xmlns:mc="/markup-compatibility/2006"     mc:Ignorable="d" xmlns:data

72、="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"    x:Class="SilverlightClient.MainPage"    d:DesignWidth="640" d:DesignHeight="480">    <Grid x:Name="LayoutRo

73、ot" Background="White" Width="320" Height="220">        <data:DataGrid x:Name="dgEmployee" Height="150" Margin="8,8,0,0" VerticalAlignment="Top"      

74、;   HorizontalAlignment="Left" Width="304"/>        <Button x:Name="btnGetData" Height="28" HorizontalAlignment="Left" Margin="8,171,0,0"        

75、0; VerticalAlignment="Top" Width="98" Content="Get Data"/>    </Grid></UserControl>MainPage.xaml.cs代码:using .System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;

76、using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Data.Services.Client;/引入System.Data.Services.Client命名空间using SilverlightClient.EmployeeWCFServiceReference;/引入数据服务所在命名空间namespace Silverl

77、ightClient.    public partial class MainPage : UserControl    .        public MainPage()        .            InitializeC

78、omponent();            /注册事件触发处理              this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click);         

79、0;      void btnGetData_Click(object sender, RoutedEventArgs e)        .            EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient(); 

80、0;          webClient.GetEmployeesAsync();/异步调用              webClient.GetEmployeesCompleted += new EventHandler(webClient_GetEmployeesCompleted);             

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论