I have a problem with data bound properties of a textbox mapped to a custom class, for the most part it works without problems; when i update a value in the bound object the changes appear in the textbox Text property, if i set the objects property to null then it gets properly formatted to "-No Data-", but when i set the object as null the Text value in the textbox remains without changes.
I have 2 classes
Class1
Class2
Class1 implements INotifyPropertyChanged in the following way:
public class Class1: INotifyPropertyChanged
{
private string _serial;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
[DisplayName("Serial Number")]
public string SerialNumber
{
get { return _serial; }
set
{
_serial = value;
OnPropertyChanged("SerialNumber");
}
}
}
Class2 inherits from Class1:
public class Class2: Class1
{
//My props & methods, etc.
}
In my form i bind the textbox Text property in the following way:
Class2 myobject = new Class2();
myobject.SerialNumber = "1234";
//I only need the textbox to display, not update the datasource
//so i use DataSourceUpdateMode.Never
mytextbox.DataBindings.Add("Text", myobject, "SerialNumber",true,DataSourceUpdateMode.Never,"-No Data-");
Later on if i do the following assignments in the object this is what happens:
myobject.SerialNumber = "123"; //mytextbox Text is "123"
myobject.SerialNumber = "456"; //mytextbox Text changes to "456"
myobject.SerialNumber = "123"; //mytextbox Text is "123"
myobject.SerialNumber = null; //mytextbox Text changes to "-No Data-"
myobject.SerialNumber = "123"; //mytextbox Text is "123"
myobject = null; //mytextbox Text remains "123" <-this is the problem
Shouldn't the textbox Text change to "-No Data-" since the whole object is null?
Am i missing something to properly display the desired data?