开发者

Why don't these DataBindings update?

开发者 https://www.devze.com 2023-02-10 22:42 出处:网络
I have created a class for adding numbers: public class Add { private int num1; public int Num1 { get { return num1; }

I have created a class for adding numbers:

public class Add 
{
    private int num1;
    public int Num1
    {
        get { return num1; }
        set { num1 = value; }
    }
    private int num2;
    public int Num2
    {
        get { return num2; }
        set { num2 = value; }
    }
    public int Result
    {
        get { return num1 + num2; }
    }
}

And created TextBox to binding the result to:

public partial class Form1 : Form
{
    Add add = new Add();

    public Form1()
    {
        InitializeComponent();

        textBox1.DataBindings.Add("text", add, "Result");
    }
... 

I change the property by catching the Click event:

private void Form1_Click(object sender, EventArgs e)
{
    add.Num1++;
   开发者_开发技巧 MessageBox.Show(add.Result.ToString());
} 

In this case, the MessageBox shows the correct value. But textBox1 still contains the old value. Why are the DataBindings not working in this code?

PS. sorry for my weak English.


Because you never told it to.
The databinding infrastructure cannot magically detect when your property changes.

You need to implement the INotifyPropertyChanged interface and raise the PropertyChanged event whenever any property changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消