Binding to WPF Controls
In this section, I’ll talk about binding your configuration object to WPF controls. As WPF provides rich binding infrastructure, Cinchoo framework take a step closer to bind the configuration object to WPF controls seamlessly. Lets walk over how you can achieve this.
For a sample configuration object below
[ChoNameValueConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
[ChoPropertyInfo("name", DefaultValue="Mark")]
public string Name;
[ChoPropertyInfo("message", DefaultValue="Hello World!")]
public string Message;
}
You can bind the configuration object to your WPF window in either constructor or window loaded event handler as below.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ChoWPFBindableConfigObject<SampleConfigSection> bindObj = new ChoWPFBindableConfigObject<SampleConfigSection>();
this.DataContext = bindObj;
}
That’s it. Now you can bind each control to this context object members. For example, the below snippet binds a text box to ‘Name’ property of above configuration object.
<TextBox Name="txtValidExts" Text="{Binding Path=Name}" />
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.