DataBinding does not work in a C # winform textbox

0

I request your kind cooperation with the following, I have a student class and a form with 2 textbox, in the load of the form I declare the DataBindings property of each textbox with the attributes of the class, however when setting an attribute of the class , the value in the textbox or visverza object is not updated.

public class alumno : INotifyPropertyChanged
{

private string _nombre;
private string _apellido;

public event PropertyChangedEventHandler PropertyChanged;

public string Nombre { get {return _nombre;} set { _nombre=value; NotifyPropertyChanged();}

public string Apellido { get {return _apellido;} set { _apellido=value; NotifyPropertyChanged();}

private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        } 

}

 private void FrmPptoIndRed_Load(object sender, EventArgs e)
{

txtNombre.DataBindings.Add("Text", _alumno, "Nombre");
txtApellido.DataBindings.Add("Text", _alumno, "Apellido");

_alumno.Nombre = "Pedro"; //no actualiza el textbox
txtApellido.Text = "Picapiedra"; //no actualiza el atributo de la clase _alumno.Apellido

}

I do not know what I am doing wrong or what is missing. Thanks.

    
asked by Jesus Bocanegra 07.12.2018 в 20:28
source

1 answer

1

It is because your method of data link is by default in control validation, add the following code so that the update is every time the linked property is modified:

txtNombre.DataBindings.Add("Text", _alumno, "Nombre");
txtNombre.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
txtApellido.DataBindings.Add("Text", _alumno, "Apellido");
txtApellido.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;

Microsoft Documentation

Annex:

Testing the code works correctly on my side this is the way I handle it because I do not have access to c # 6.0 on the machine I'm in right now:

public class alumno : INotifyPropertyChanged
{

    private string _nombre;
    private string _apellido;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Nombre
    {
        get { return _nombre; }
        set
        {
            _nombre = value;
            NotifyPropertyChanged();
        }
    }

    public string Apellido
    {
        get { return _apellido; }
        set
        {
            _apellido = value;
            NotifyPropertyChanged();
        }
    }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }        

}

In the form load event:

 private void FrmPptoIndRed_Load(object sender, EventArgs e)
{
    var _alumno = new alumno(); //instanciamos la clase de alumno
     _alumno.Nombre = "Pedro"; //no actualiza el textbox
     _alumno.Apellido = "Picapiedra";
     //textBox2.Text = "Picapiedra"; //no hay razon para hacer esto, usas la propiedad _alumno.Apellido
      textBox2.DataBindings.Add("Text", _alumno, "Apellido");
      textBox2.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
    textBox1.DataBindings.Add("Text", _alumno, "Nombre");
    textBox1.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
}
    
answered by 07.12.2018 в 20:40