简体   繁体   English

使用Silverlight和WCF服务的双向绑定

[英]Two way binding using Silverlight and WCF Service

To simplify the question, I have a single textbox control which I want to apply two way binding. 为了简化问题,我有一个文本框控件,我想应用两种方式绑定。 I have an object class named Customer for example on the server side, the silverlight application is obviously a separate project. 我在服务器端有一个名为Customer的对象类,例如,Silverlight应用程序显然是一个单独的项目。

public class Customer
{
    public string Name{ get; set; }
}

In the WCF Service I have query which populates the customer name, and returns a list of type Customer. 在WCF服务中,我有一个查询,该查询将填充客户名称,并返回类型为Customer的列表。

List<Customer> data = new List<Customer>();

On the client side I then have this bound to the textbox control: 然后,在客户端,我将其绑定到文本框控件:

<TextBox Canvas.Left="345" Canvas.Top="12" Height="23" Name="tb_customer" Width="120" Text="{Binding Path=Name}" />

List<ServiceReference.Customer> data = e.Result;
tb_customer.DataContext = data[0];

This is working fine, and is binding the customer name to the textbox control. 这工作正常,并将客户名称绑定到文本框控件。 But my question is, when I change the value on the client side how do I go about sending the modified customer name back to the data source, in this case a table named customers in sqlserver. 但是我的问题是,当我在客户端更改值时,如何将修改后的客户名称发送回数据源,在本例中为sqlserver中名为customers的表。 Would I need to implement INotifiyPropertyChanged on the customer class? 我是否需要在客户类别上实现INotifiyPropertyChanged? But obviously the customer class is sitting on the server side, so do I need to create a local instance of the customer class on the client side, and upload these changes via the wcf service back to the server? 但是很明显,客户类位于服务器端,因此我是否需要在客户端上创建客户类的本地实例,并通过wcf服务将这些更改上传回服务器?

Set the Textbox up like this: 像这样设置文本框:

<TextBox Canvas.Left="345" Canvas.Top="12" Height="23" Name="tb_customer" Width="120" Text="{Binding Path=Name, Mode=TwoWay}" />

With this setup, changes made to the value of the textbox will automatically update the state of the object , locally. 使用此设置,对文本框的值所做的更改将自动在本地更新对象的状态。 You will then have to commit the changes back to the datastore. 然后,您将不得不将更改提交回数据存储。 If you are using an ORM such as Entity Framework, that's easy- just call SubmitChanges() on your context and you're done. 如果您使用的是诸如实体框架之类的ORM,只需在上下文中调用SubmitChanges()就可以了。 If you're not using an ORM, you'll have to handle the update manually. 如果您不使用ORM,则必须手动处理更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM