How to best implement this two-way binding pattern?

Jul 27, 2008 at 6:00 AM
Edited Jul 27, 2008 at 6:02 AM

Hi guys,

First of all, I love Umbrella!

Here's my issue: I'm trying to make my application settings <-> form controls binding nice and smooth. So I have this method, which I'm using like this:

Settings.Default.Bind("SettingName", this.chkSetting);

(where this particular overload takes a CheckBox and thus binds to the Checked property; the more general one takes a Control and a second parameter for which property it binds to.) This is simple, just translating to

theControl.DataBindings.Add(controlProperty, this, settingName);

But now I want to be able to write code like this:

Settings.Default.Bind("SettingName", new Dictionary<RadioButton, SettingEnum>
{
{ this.radSettingNameA, SettingEnum.A},
{ this.radSettingNameB, SettingEnum.B },
{ this.radSettingNameC, SettingEnum.C }
});

My current attempt, which I haven't tested yet for unrelated reasons, is the following method of my Settings class (which implements ApplicationSettingsBase, and thus IPropertyChanged, and has strongly-typed properties for each setting):

public void Bind<T>(string settingName, Dictionary<RadioButton, T> controlValueMap)
{
var value = new ReflectionValue<T>(this, settingName);
value.Observe(
(sender, args) =>
{
foreach (var pair in controlValueMap)
{
pair.Key.Checked = value.Get().Equals(pair.Value);
}
}
);

foreach (RadioButton rad in controlValueMap.Keys)
{
var radValue = new ReflectionValue<bool>(rad, "Checked");
radValue.Observe(
(sender, args) =>
{
if (radValue.Get())
{
value.Set(controlValueMap[rad]);
}
}
);
}
}
This is kinda messy, creates some ReflectionValue<T> objects which apparently need to be disposed in a way that I don't understand (will they automatically dispose after going out of scope, thus breaking everything? Confused.), and it feels like you guys probably have a really elegant way of using Umbrella to make this sort of thing happen, probably by using some kind of two-way binding instead of two one-way bindings.

Thoughts?