简体   繁体   中英

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.

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.

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. Would I need to implement INotifiyPropertyChanged on the customer class? 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?

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. If you're not using an ORM, you'll have to handle the update manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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