2009-10-26 43 views
2

我需要的文本框,这将反映数据绑定字符串的变化。我尝试了以下代码:数据绑定文本框并不反映源变化

public partial class Form1 : Form 
{ 
    string m_sFirstName = "Brad"; 
    public string FirstName 
    { 
     get { return m_sFirstName; } 
     set { m_sFirstName = value; } 
    } 

    public Form1() 
    { 
     InitializeComponent(); 

     textBox1.DataBindings.Add("Text", this, "FirstName"); 
    } 

    private void buttonRename_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("before: " + FirstName); 
     FirstName = "John"; 
     MessageBox.Show("after: " + FirstName); 
    } 
} 

启动应用程序后,textBox1正确填充了Brad。 我点击按钮,改名姓为“约翰”(第二消息框证实了这一点)。 但textBox1仍然充满了布拉德,而不是约翰。为什么?什么会使这项工作?

回答

3

为什么数据绑定不反映你的变化的原因是因为你要绑定哪些没有被设计修改时,抛出事件简单的System.String对象。

所以,你有两个选择。一种是在按钮的Click事件中重新绑定值(请避免!)。另一种是创建一个自定义类,将执行INotifyPropertyChanged是这样的:

public partial class Form1 : Form 
{ 
    public Person TheBoss { get; set; } 

    public Form1() 
    { 
     InitializeComponent(); 

     TheBoss = new Person { FirstName = "John" }; 

     textBox1.DataBindings.Add("Text", this, "TheBoss.FirstName"); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     TheBoss.FirstName = "Mike"; 
    } 


    public class Person : INotifyPropertyChanged 
    { 
     private string firstName; 

     public string FirstName 
     { 
      get 
      { 
       return firstName; 
      } 
      set 
      { 
       firstName = value; 
       NotifyPropertyChanged("FirstName"); 
      } 
     } 

     private void NotifyPropertyChanged(String info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

     #region INotifyPropertyChanged Members 

     public event PropertyChangedEventHandler PropertyChanged; 

     #endregion 
    } 
} 

INotifyPropertyChanged的文档:MSDN

0

您需要在点击按钮再次进行绑定,否则只有在形式实例运行一次。

补充:上面的语句是不是要求是正确的。它仍然有效(见下面的代码),但是通过事件处理失败了绑定的目的。

Binding binding1; //binding instance 
public Form1() 
{ 
    InitializeComponent(); 
    binding1 = textBox1.DataBindings.Add("Text", this, "FirstName"); //assign binding instance 
} 

private void buttonRename_Click(object sender, EventArgs e) 
{ 
    MessageBox.Show("before: " + FirstName); 
    FirstName = "John"; 
    textBox1.DataBindings.Remove(binding1); //remove binding instance 
    binding1 = textBox1.DataBindings.Add("Text", this, "FirstName"); //add new binding 
    MessageBox.Show("after: " + FirstName); 
} 
+0

我在我的例子了文本框,但也有数据绑定控件只读(列表框,标签,。 ..)。真的是“label1.DataBindings.Add(”Text“,this,”FirstName“);”与“label1.Text = FirstName;”相同???我仍然相信必须存在双面约束。 – jing 2009-10-26 09:21:13

+0

@Jing,我的回答不适合您的需要。为了工作,需要在添加另一个实例之前删除数据绑定实例。其他答案给了你更好的设计。 – 2009-10-26 10:07:17

3

一种方法是添加一个FirstNameChanged事件,该事件将绑定数据绑定。然后在您更改财产时提出事件,并重新绑定。例如:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

public class DataBindingTest : Form 
{ 
    public event EventHandler FirstNameChanged; 

    string m_sFirstName = "Brad"; 
    public string FirstName 
    { 
     get { return m_sFirstName; } 
     set 
     { 
      m_sFirstName = value; 
      EventHandler handler = FirstNameChanged; 
      if (handler != null) 
      { 
       handler(this, EventArgs.Empty); 
      } 
     } 
    } 

    public DataBindingTest() 
    { 
     Size = new Size(100, 100); 
     TextBox textBox = new TextBox(); 
     textBox.DataBindings.Add("Text", this, "FirstName"); 

     Button button = new Button 
     { 
      Text = "Rename", 
      Location = new Point(10, 30) 
     }; 
     button.Click += delegate { FirstName = "John"; }; 
     Controls.Add(textBox); 
     Controls.Add(button); 
    } 

    static void Main() 
    { 
     Application.Run(new DataBindingTest()); 
    } 
} 

有可能是(例如使用INotifyPropertyChanged)做它的其他方式 - 我不以任何方式数据绑定专家。