2013-04-18 96 views
1

假设我在窗体中有一个String属性(并且窗体没有实现INotifyPropertyChanged)。我也创建了一个BindingSource并将其DataSource设置为表单。然后,我将一个文本框绑定到表单上的String属性(间接使用BindingSource)。简单的Windows窗体数据绑定

问题1:当我在运行时更改文本框中的值时,为什么我不在String属性的setter中创建断点?我认为将控件绑定到String属性会允许在这个方向上自动更新(GUI - >成员数据)

问题2:如何在另一个方向触发更新(成员数据 - > GUI)当GUI以外的其他内容更改String属性时会发生?我不想实现INotifyPropertyChanged接口并将NotifyPropertyChanged添加到setter。我认为,通过使用的BindingSource的ResetBindings我至少可以触发这个手动

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!" 
    } 
} 

回答

6
this.bindingSource1.DataSource = this; 

我想你忘记指定数据源。

+0

完美!设计人员使用此声明而不是您提供的声明: this.bindingSource1.DataSource = typeof(binding_test.Form1); – user1130698 2013-04-18 05:09:42

+0

@ user1130698问题已解决,请勾选此问题作为回答,让我们知道... – 2013-04-18 05:10:26

+0

仍然想知道问题2,如何在不实施INotifyPropertyChanged的情况下设置其他方向(成员数据 - > GUI)的自动更新。现在,ResetBindings()确实允许我手动执行此操作 – user1130698 2013-04-18 05:12:53

相关问题