Unable to correctly implement a binding between the property value of an object and the property value of a control
I'm try开发者_Go百科ing to update textedit control through the Client class object databindings with INotifyPropertyChanged implementation and i can't get it to work. The object behind (datasource) updates but the textedit still remains blank. If i type the text into the editbox the datasource gets updated. Would you help please? Here's the relevant code i'm using:
public class Client : NotifyProperyChangedBase
{
private string _firstname;
public string Firstname
{
get
{
return this._firstname;
}
set
{
this.CheckPropertyChanged<string>("Firstname", ref _firstname, ref value);
}
}
}
public Client ClientA = new Client();
Binding fname = new Binding("Text", ClientA, "Firstname", true, DataSourceUpdateMode.OnPropertyChanged);
ultraTextEditor_firstname.DataBindings.Add(fname);
ClientA.Firstname = "testN"; <== editbox remains blank ...
Am I missing something here? Thanks in advance, Peter.
I am assuming your base is implemented something along the lines of this example. If I am incorrect in my assumption, you will need to provide the implementation of your NotifyProperyChangedBase class.
You may also want to review the Binding(String, Object, String, Boolean, DataSourceUpdateMode) constructor documentation, as it discusses the control events the binding attempts to locate.
Looking at that example, you will want to try something like this:
System.ComponentModel.BindingList<Client> bindings = new System.ComponentModel.BindingList<Client>();
Client clientA = bindings.AddNew();
clientA.Firstname = "John";
textEditControl.DataSource = bindings;
// This change presumably will be refelected in control
clientA.Firstname = "Jane";
Update: After reviewing the documentation on the Add method of the ControlBindingsCollection class; I believe that the data source of the Binding needs to implement the IListSource interface in order to properly participate in the binding (all MSDN examples are DataSet or DataTable which implement this interface).
精彩评论