如果已经有人问过这个问题,我很抱歉但是我找不到。 我正在尝试将一个文本框绑定到数据源,但是当数据源更改时文本框没有更新... 这是我的代码,有人能建议我哪里做错了吗? 谢谢!
public Controller()
{
myForm.databaseTextBinding = new Binding("Text", ac, "connString");
myForm.databaseTextBinding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
myForm.setupSources();
}
public partial class Form1 : Form
{
public Binding databaseTextBinding;
public void setupSources()
{
DatabaseTextBox.DataBindings.Add(databaseTextBinding);
}
}
更新:
我现在可以创建能够更新的文本框。但新的问题出现了。数据源本身通过我创建一个新的AC对象进行更改。但是如果我这样做,绑定将不再更新。考虑过可能属性未改变,但我确定它已经改变了!
例如,这个有效。
ac.cString = "ABC";
ac.cString = "DEF";
但这并不意味着...
ac = new AccessConnector(path);
ac.cString = "ABC";
ac.cString = "DEF";
public event PropertyChangedEventHandler PropertyChanged;
和PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyProperty)));
,却忘记在类定义中添加: INotifyPropertyChanged
,仍然很容易被这个问题困扰。别问我怎么知道的 :) - amonroejj