I use Winforms Databinding and I have derived classes, where the base class implements IPropertychanged
:
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName) {
var handler = this.PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Each propertysetter calls:
protected void SetField<T>(ref T field, T value, string propertyName) {
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
IsDirty = true;
this.RaisePropertyChanged(propertyName);
}
}
A typical Propertysetter:
public String LocalizationItemId {
get {
return _localizationItemId;
}
set {
SetField(ref _localizationItemId, value, "LocalizationItemId");
}
}
The way a property is bound to a textbox
private DerivedEntity derivedEntity
TextBoxDerivedEntity.DataBindings.Add("Text", derivedEntity, "Probenname");
If I programmatically assign text to the textbox, the textbox does not show it. But I can manually edit the textbox.
I know it is too late to answer, but this problem can be solved, if you set event when your binding should change value, if you set it on property value change event your problem will be solved. You can do this by this way
textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty", true, DataSourceUpdateMode.OnPropertyChanged);
Binding source is updated on TextBox Validated event. TextBox validated event is called when user edit TextBox and then changes focus to other control. Since you're changing TextBox text programmatically TextBox doesn't know that text were changed and therefore validation is not called and binding is not updated, so you need to update binding manually.
Initialize binding:
var entity;
textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty");
Change TextBox.Text:
textBox.Text = "SOME_VALUE";
Update binding manually:
textBox.DataBindings["textBoxProperty"].WriteValue();
Binding.WriteValue() reads value from control and updates entity accordingly. You could read about WriteValue at MSDN.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With