Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two way databinding in winforms, Inotifypropertychanged implemented in base class

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.

like image 208
traveller Avatar asked Oct 19 '13 15:10

traveller


2 Answers

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);
like image 200
Haider Ali Wajihi Avatar answered Sep 18 '22 07:09

Haider Ali Wajihi


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.

like image 25
Ryogo Avatar answered Sep 18 '22 07:09

Ryogo