简体   繁体   中英

Simple Windows Forms data binding

Let's say I have a String property in my form (and the form does not implement INotifyPropertyChanged). I've also created a BindingSource and set its DataSource to the form. Then I bind a textbox to the String property on my form (indirectly, using the BindingSource).

Question 1: When I change the value in the textbox at runtime, why don't I hit a breakpoint in the setter of the String property? I thought that binding the control to the String property would allow updates in this direction (GUI -> member data) to occur automatically

Question 2: How can I trigger updates in the other direction (member data -> GUI) to occur when something other than the GUI changes the String property? I don't want to implement the INotifyPropertyChanged interface and add NotifyPropertyChanged to the setter. I thought that by using the BindingSource's ResetBindings I could at least trigger this manually

public partial class Form1 : Form
{
    private String m_blah;
    public String Blah
    { 
        get
        {
            return m_blah;
        }
        set
        {
            m_blah = value;
        }
    }

    public Form1()
    {
        InitializeComponent();
        textBox1.DataBindings.Add(new Binding("Text", bindingSource1, "Blah",true,DataSourceUpdateMode.OnValidation));
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Blah = "Clicked!";
        this.bindingSource1.ResetBindings(false); //expecting the GUI to update and say "Clicked!"
    }
}
this.bindingSource1.DataSource = this;

我认为您忘记分配数据源。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM