使用双向DataBinding的Winforms Textbox不能正常工作

cha*_*ans 2 c# binding textbox

Winforms有两个Textboxes.textBox2绑定到财产单位.我希望对Unit进行的任何更改或textBox2textBox2分别自动更新或Unit.但事实并非如此.以下是Winform代码的三个版本.

版本一设置数据绑定,希望它有两种方式自动更新但不起作用

public partial class Receiver : Form, INotifyPropertyChanged
{         

    private int unit=0;
    public int Unit
    {
        get { return unit; }
        set
        {
            if (value != unit)
            {
                unit = value;                                          
            }
        }
    }

    public Receiver()
    {
        InitializeComponent();            
        textBox2.DataBindings.Add("Text", Unit, "Unit");
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {   
        //textBox1 change makes Unit change, 
        //I wish the change will be displayed in textBox2 automatically
        Unit = Convert.ToInt32(textBox1.Text);
    }        
}
Run Code Online (Sandbox Code Playgroud)

版本二与事件处理程序,硬编码到更新textBox2与事件处理程序

但更改textBox2仍然不会自动更新单位

public partial class Receiver : Form, INotifyPropertyChanged
{ 
    public event PropertyChangedEventHandler PropertyChanged;

    private int unit=0;
    public int Unit
    {
        get { return unit; }
        set
        {
            if (value != unit)
            {
                unit = value;                       
                if (PropertyChanged != null)
                {
                   PropertyChanged(this, new PropertyChangedEventArgs(info));
                }  
            }
        }
    }

    public Receiver()
    {
        InitializeComponent();            
        textBox2.DataBindings.Add("Text", this.Unit, "Unit", false, 
                                                 DataSourceUpdateMode.OnPropertyChanged);
        PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);

    }    

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        Unit = Convert.ToInt32(textBox1.Text);
    }


    private void OnPropertyChanged(object sender, EventArgs e)
    {  
        //this actually is hard coded to update textBox2, binding does no help
        textBox2.Text = Unit.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

版本三为什么使用事件处理程序,我们可以简单地这样做.

public partial class Receiver : Form, INotifyPropertyChanged
{ 
    private int unit=0;
    public int Unit
    {
        get { return unit; }
        set
        {
            if (value != unit)
            {
                unit = value;   
                textBox2.text = unit.toString();                          
            }
        }
    }

    public Receiver()
    {
        InitializeComponent();            
        textBox2.DataBindings.Add("Text", Unit, "Unit");
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        Unit = Convert.ToInt32(textBox1.Text);           
    }   

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        Unit = Convert.ToInt32(textBox2.Text);           
    } 
}
Run Code Online (Sandbox Code Playgroud)

}

版本2版本3都有问题,任何更改textbox1都会导致更新textbox2.那将导致很多CPU周期.最好的方法是当鼠标焦点离开textBox1然后进行更新.那怎么办呢?

Jal*_*aid 5

您的代码中的问题是在线:

textBox2.DataBindings.Add("Text", Unit, "Unit");
Run Code Online (Sandbox Code Playgroud)

您应该通过包含该属性而不是属性本身的实例替换数据源,此处还应指定将其设置为的数据源更新模式DataSourceUpdateMode.OnPropertyChanged.所以:

textBox2.DataBindings.Add("Text", this, "Unit", false, DataSourceUpdateMode.OnPropertyChanged);

//test using the second or the third approach:
Unit = 15;//now the textBox2.Text equals to 15 too.

textBox2.Text = 12;//now Unit equals 12 too
Run Code Online (Sandbox Code Playgroud)