Binding to WinForm controls
In this section, I’ll talk about binding your configuration object to WinForm controls. WinForm has rich binding infrastructure, Cinchoo framework works closely with WinForms to bind the configuration object to them seamlessly. Lets walk over how you can do this.
For a sample configuration object below
[ChoNameValueConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
[ChoPropertyInfo("name", DefaultValue = "Mark")]
public string Name
{
get;
set;
}
[ChoPropertyInfo("message", DefaultValue = "Hello World!")]
public string Message
{
get;
set;
}
}
PS: WinForm controls only binds to public Properties only. Please make sure you define them accordingly in your configuration object.
You can bind the above configuration object to your WinForm window in either constructor or window load event handler as below.
private void MainForm_Load(object sender, EventArgs e)
{
SampleConfigSection settings = new SampleConfigSection();
textBox1.DataBindings.Add("Text", settings, "Name");
textBox2.DataBindings.Add("Text", settings, "Message");
}
That’s all. Now the changes made to configuration source will be reflected in the controls as well as the changes made to controls will be persisted automatically to underlying source based on the binding nature. Try for yourself.