Is this the way and appeal of data binding?

Okay, let's see if I'm thinking straight:

If I simply want to read some data from a source and then apply it to some control, I may as well just do that directly rather than go to the trouble of data binding. IOW, I may as well do this:

foreach (var quad in listQH)
{
   ...
   tb.Text = quad.Ph1;

...as opposed to:

tb.DataBindings.Add(new Binding("Text", quad, "Ph1"));

However, if I want updates within the underlying class instance to update the controls (the "tb" textBox in this case), and user updates of the controls to update those class instance members ("two-way binding"), I need to implement INotifyPropertyChanged. But then, I would have to change this code:

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"));
        . . .

...to:

List<QHQuad> listQH; //global to the form
. . .
listQH = GetInfoForPlatypus(PlatypusId, dow);
foreach (var quad in listQH)
{
    // the same as above

And then I'd be able to ultimately save those potentially changed class instance values in this way:

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

Upvotes: 0

Views: 108

Answers (1)

Malgaur
Malgaur

Reputation: 1850

You have the right idea. Here are a few pointers, though.

INotifyPropertyChanged is only required if you want changes from within the ViewModel to bubble up to the view. It is not required for two-way binding. If you want two way binding and only need to read once when view loads, a plain property is fine.

You may want to check out the MVVM (Model - View - ViewModel) pattern. It is the recommended design pattern for WPF, Silverlight, Metro, etc. because it is great for data-binding heavy implementations.

Upvotes: 2

Related Questions