好吧,让我们看看我是否在想:
如果我只是想从源中读取一些数据,然后将其应用到某个控件,我不妨直接这样做,而不是去麻烦数据绑定。IOW,我不妨这样做:
foreach (var quad in listQH)
{
...
tb.Text = quad.Ph1;
...相对于:
tb.DataBindings.Add(new Binding("Text", quad, "Ph1"));
但是,如果我希望在基础类实例中更新以更新控件(在本例中为“tb”文本框),并且用户更新控件以更新这些类实例成员(“双向绑定”),我需要实施 INotifyPropertyChanged。但是,我将不得不更改此代码:
List<QHQuad> listQH = GetForPlatypus(PlatypusId, dow); // listQH locally declared
foreach (var quad in listQH)
{
int QHCell = quad.QH;
if ((QHCell >= 1) || (QHCell <= QUARTER_HOUR_COUNT))
{
string PH1CellToPopulate = string.Format("textBoxA_{0}", QHCell);
string PH2CellToPopulate = string.Format("textBoxB_{0}", QHCell);
string PH3CellToPopulate = string.Format("textBoxC_{0}", QHCell);
var tb = (TextBox)this.Contro.Find(PH1CellToPopulate, true).First();
tb.DataBindings.Add(new Binding("Text", quad, "Ph1"));
. . .
...到:
List<QHQuad> listQH; //global to the form
. . .
listQH = GetInfoForPlatypus(PlatypusId, dow);
foreach (var quad in listQH)
{
// the same as above
然后我最终能够以这种方式保存那些可能更改的类实例值:
foreach (var quad in listQH)
{
UpdateQH(quad); // quad contains members QH, Ph1, Ph2, and Ph3 ("UPDATE BLA SET PH1 = :Ph1, PH2 = :Ph2, PH3 = :Ph3 WHERE QH = :QH")
}