I bind a property to a textbox control (Windows form program), using the following code:
textBox.DataBindings.Add("Text", myObject, myPropertyName, false, DataSourceUpdateMode.OnPropertyChanged);
Further along in the program, I update the value of the property by reflection – using this code.
PropertyInfo propertyInfo = myObject.GetType().GetProperty(myPropertyName);
propertyInfo.SetValue(myObject, myNewValue, null);
The property’s value is being updated in the object. I can see it in the debugger, when the code returns to the form. However, the textbox does not show the new value.
If I clear the databinding on the textbox, and re-add it, it does show the new value.
How can I get the databound textbox to automatically display the new value, after setting the property value by reflection?
Does the binding work if you set it not using reflection? If it is still not working then you probably need to have your object implement INotifyPropertyChanged and then in your setter you need to raise the OnPropertyChanged event. This is how it works in WPF, I presume windows forms is the same or similar. Basically the binding needs a way to know that the property has changed, it does so by handling that event.