C# – Winforms .Net Simple Databinding Not Working

.netc++data-bindingwinforms

I have a Winform with a very basic premise: modify 2 string properties of an object that is passed into it, then save it to disk when the form closes. I am trying to use databinding to bind the Text properties of 2 textboxes on the form to the 2 string properties of the object.

But it isn't working. The textboxes never display the values I am assigning to the object properties in the constructor. And when I type something into the textboxes, the object properties are not getting updated. What am I doing wrong?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Eds_Viewer
{
    public partial class EdsConfigForm : Form
    {
        public EdsConfigForm(EdsConfig myconfig)
        {
            InitializeComponent();
            EdsConfig = myconfig;
            if (EdsConfig.VFPConnectionString == null) //set a default value
            {
                EdsConfig.VFPConnectionString = "Provider=vfpoledb;Data Source=g:\\eds\\";
            }
            if (EdsConfig.VFPFileName == null) //set a default value
            {
                EdsConfig.VFPFileName = "InvoiceDB";
            }
            this.VFPConnectionStringTextbox.DataBindings.Add("Text", EdsConfig, "VFPConnectionString");
            this.VFPFileNameTextbox.DataBindings.Add("Text", EdsConfig, "VFPFileName");
        }
        EdsConfig EdsConfig;
        private void SaveConfigToDisk(EdsConfig myconfig)
        {
            XmlSerializer x = new XmlSerializer(typeof(EdsConfig));
            TextWriter tw = new StreamWriter("EdsConfig.xml");
            x.Serialize(tw, myconfig);
            tw.Close();
        }
        private void EdsConfigForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.SaveConfigToDisk(this.EdsConfig);
        }
    }
}

Best Solution

Does the EdsConfig class implement the INotifyPropertyChanged interface?
This is a requirement for objects that are used as databinding sources, since the PropertyChanged event raised from the properties' setters is used to update the bindings whenever the property is modified.

Related Question