vendredi 31 mai 2019

DataBinding to a checkbox not working correctly

I have a problem using DataBinding to a checkbox in C#. Checkbox does not reflect the value of the object it is bind to.

I used "checkBox1.DataBindings.Add("Checked", cb, "t", false, DataSourceUpdateMode.OnPropertyChanged)" to bind a simple data source to an object, cb. The the bool property "t" in the object updates based on the checkbox checked state, but the checkbox check state does not reflect the object state (cb.t).

namespace WindowsFormsApplication1 { public partial class Form1 : Form { test cb = new test(); public Form1() { InitializeComponent();

        cb.t = false;

        checkBox1.DataBindings.Add("Checked", cb, "t", false, DataSourceUpdateMode.OnPropertyChanged);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        cb.t = true;
        //checkBox1.Refresh();
        //checkBox1.Invalidate();
    }
}

public class test
{
    public bool t { set; get; }
}

}

if i click button1 the state of the checkbox does not change; however, the if i i set the cb.t = true in the constructor when the form loads the checkbox checked state is the same as the value of cb.t. thanks for the help.




Aucun commentaire:

Enregistrer un commentaire