您的位置:首页 > 其它

有关DataForm组件的研究_基础知识和实现服务端批量CURD——Silverlight学习笔记[23]

2011-01-10 10:55 921 查看

DataForm组件在开发中,往往用于单个数据项的添加、更新、删除的操作。本文将为大家介绍DataForm组件的基础应用知识以及通过Linq to SQL+Silverlight-enabled WCF Web Service进行与数据库的批量CURD交互。

组件所在命名空间:

System.Windows.Controls

组件的常用属性:

AutoCommit:获取或设置一个值来表示当当前项被改变时已被编辑的数据项是否被提交。

AutoEdit:获取或设置一个值来表示DataForm是否应当被永久置于编辑模式。

AutoGenerateFields:获取或设置一个值来表示DataForm是否自动生成数据域集合。

CancelButtonContent:获取或设置取消按钮的内容。

CancelButtonStyle:获取或设置取消按钮的样式。

CommandButtonsVisibility:获取或设置一个值用来表示在DataForm面板上的命令按钮是否可见。

CommitButtonContent:获取或设置提交按钮的内容。

CommitButtonStyle:获取或设置提交按钮的样式。

CurrentIndex:获取或设置当前项的索引值。

DataFieldStyle:获取或设置数据域的具体样式。

DescriptionViewerPosition:获取或设置一个值用来表示与当前数据域相关的描述内容的位置。

EditTemplate:获取或设置当DataForm处于编辑模式时的编辑模板。

Header:获取或设置DataForm的头部。

HeaderTemplate:获取或设置DataForm的头部的头模板。

HeaderVisibility:获取或设置一个值用来表示DataForm的头部是否可见。

IsEmpty:获取一个值用来表示该控件是否能显示一个数据项。

IsItemChanged:获取一个值用来表示当前数据项是否被更改。

IsItemValid:获取一个值用来表示当前数据项是否被有效。

IsReadOnly:获取一个值用来表示DataForm组件是否处于只读状态。

LabelPosition:获取或设置一个值用来表示与当前数据域相关的标签的位置。

Mode:获取一个值用来表示DataForm组件是否处于只读、编辑或新增项目状态。

NewItemTemplate:获取或设置一个项模板当添加新数据项时。

ReadOnlyTemplate:获取或设置一个项模板当DataForm组件处于只读状态时。

ValidationSummary:获取验证结果汇总。

ValidationSummaryStyle:获取或设置验证结果汇总的具体样式。

组件的常用方法:

AddNewItem:添加一个新数据项。

BeginEdit:开始当前项的编辑模式。

CancelEdit:取消当前项的编辑模式。

CommitEdit:提交更改。

DeleteItem:删除当前项。

FindNameInContent:在DataForm中寻找一个指定名称的对象。

ValidateItem:验证当前项是否有效。

组件常用事件:

AddingNewItem:当添加新数据项时发生。

AutoGeneratingField:当一个数据域自动生成时发生。

BeginningEdit:编辑启动时发生,当AutoEdit属性为true时,该状态不能被取消。

ContentLoaded:当DataForm的内容加载完毕时发生。

ContentLoading:当DataForm的内容快要加载完毕时发生。

CurrentItemChanged:在当前项改变时发生。

DeletingItem:当一个数据项正在被删除时发生。

EditEnded:在一个数据项编辑结束时发生。

EditEnding:在一个数据项编辑正在结束时发生。

ValidatingItem:在一个数据项正在被验证时发生。

实例

准备工作

1)建立起测试项目

2)创建测试用数据库

细节详情请见强大的DataGrid组件[2]_数据交互之ADO.NET Entity Framework——Silverlight学习笔记[10]

创建Linq to SQL 数据模型类

细节详情请见强大的DataGrid组件[3]_数据交互之Linq to SQL——Silverlight学习笔记[11]

建立Silverlight-enabled WCF Service数据通信服务

建立的过程参见强大的DataGrid组件[3]_数据交互之Linq to SQL——Silverlight学习笔记[11]。然而,必须要将文件EmployeeInfoWCFService.svc.cs修改为如下所示:

using 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 dataform

{

[ServiceContract(Namespace = "")]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class EmployeeInfoWCFService

{

[OperationContract]

public List<Employees> GetEmployees()

{

EmployeeModelDataContext db = new EmployeeModelDataContext();

return db.Employees.ToList();

}

[OperationContract]

public void Insert(List<Employees> employee)

{

EmployeeModelDataContext db = new EmployeeModelDataContext();

employee.ForEach(x =>

{

string employeename = x.EmployeeName;

int employeeage = x.EmployeeAge;

string strsql = "INSERT INTO Employee(EmployeeName,EmployeeAge) VALUES('" + employeename + "'," + employeeage.ToString() + ")";

db.ExecuteCommand(strsql);

});

}

[OperationContract]

public void Update(List<Employees> employee)

{

EmployeeModelDataContext db = new EmployeeModelDataContext();

employee.ForEach(x =>

{

int employeeid = x.EmployeeID;

string employeename = x.EmployeeName;

int employeeage = x.EmployeeAge;

string strsql = "UPDATE Employee SET EmployeeName='" + employeename + "',EmployeeAge=" + employeeage.ToString() + "WHERE EmployeeID=" + employeeid.ToString();

db.ExecuteCommand(strsql);

});

}

[OperationContract]

public void Delete(List<Employees> employee)

{

EmployeeModelDataContext db = new EmployeeModelDataContext();

employee.ForEach(x =>

{

int employeeid = x.EmployeeID;

string strsql = "DELETE FROM Employee WHERE EmployeeID=" + employeeid.ToString();

db.ExecuteCommand(strsql);

});

}

// Add more operations here and mark them with [OperationContract]

}

}

创建SilverlightClient界面及组件代码

MainPage.xaml文件代码

<UserControl

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" xmlns:dataFormToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit" x:Class="SilverlightClient.MainPage"

d:DesignWidth="320" d:DesignHeight="240">

<Grid x:Name="LayoutRoot" Width="320" Height="240" Background="White">

<dataFormToolkit:DataForm x:Name="dfEmployee" Margin="8,8,8,58"/>

<Button x:Name="btnGetData" Height="26" Margin="131,0,110,18" VerticalAlignment="Bottom" Content="Get Data" Width="79"/>

<Button x:Name="btnSaveAll" Height="26" Margin="0,0,8,18" VerticalAlignment="Bottom" Content="Save All" HorizontalAlignment="Right" Width="79"/>

<TextBlock x:Name="tbResult" Height="26" HorizontalAlignment="Left" Margin="8,0,0,18" VerticalAlignment="Bottom" Width="108" TextWrapping="Wrap"/>

</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 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 SilverlightClient.EmployeeWCFServiceReference;

namespace SilverlightClient

{

public partial class MainPage : UserControl

{

int originalNum;//记录初始时的Employee表中的数据总数

ObservableCollection<Employees> deletedID = new ObservableCollection<Employees>();//标记被删除的对象

public MainPage()

{

InitializeComponent();

this.Loaded += new RoutedEventHandler(MainPage_Loaded);

this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click);

this.btnSaveAll.Click += new RoutedEventHandler(btnSaveAll_Click);

this.dfEmployee.DeletingItem += new EventHandler<System.ComponentModel.CancelEventArgs>(dfEmployee_DeletingItem);

}

void dfEmployee_DeletingItem(object sender, System.ComponentModel.CancelEventArgs e)

{

deletedID.Add(dfEmployee.CurrentItem as Employees);//正在删除时,将被删除对象进行标记,以便传给服务端真正删除。

}

void btnSaveAll_Click(object sender, RoutedEventArgs e)

{

List<Employees> updateValues = dfEmployee.ItemsSource.Cast<Employees>().ToList();

ObservableCollection<Employees> returnValues = new ObservableCollection<Employees>();

if (updateValues.Count > originalNum)

{

//添加数据

for (int i = originalNum; i <= updateValues.Count - 1; i++)

{

returnValues.Add(updateValues.ToArray()[i]);

}

EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();

webClient.InsertCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webClient_InsertCompleted);

webClient.InsertAsync(returnValues);

//必须考虑数据集中既有添加又有更新的情况

returnValues.Clear();

updateValues.ForEach(x => returnValues.Add(x));

webClient.UpdateCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webClient_UpdateCompleted);

webClient.UpdateAsync(returnValues);

}

else if (updateValues.Count < originalNum)

{

//删除数据

EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();

webClient.DeleteCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webClient_DeleteCompleted);

webClient.DeleteAsync(deletedID);

}

else

{

//更新数据

updateValues.ForEach(x => returnValues.Add(x));

EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();

webClient.UpdateCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webClient_UpdateCompleted);

webClient.UpdateAsync(returnValues);

}

}

void webClient_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)

{

tbResult.Text = "更新成功!";
GetEmployees();

}

void webClient_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)

{

tbResult.Text = "删除成功!";

}

void webClient_InsertCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)

{

tbResult.Text = "添加成功!";

}

void btnGetData_Click(object sender, RoutedEventArgs e)

{

GetEmployees();

}

void MainPage_Loaded(object sender, RoutedEventArgs e)

{

GetEmployees();

}

void GetEmployees()

{

tbResult.Text = "";

EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();

webClient.GetEmployeesCompleted +=

new EventHandler<GetEmployeesCompletedEventArgs>(webClient_GetEmployeesCompleted);

webClient.GetEmployeesAsync();

}

void webClient_GetEmployeesCompleted(object sender, GetEmployeesCompletedEventArgs e)

{

originalNum = e.Result.Count;//记录原始数据个数

dfEmployee.ItemsSource = e.Result;

}

}

}

最终效果图



转自:/article/4926501.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐