Consider the following:
public interface IHaveProperties
{
public MyProperties Properties { get; set; }
}
public class MyControlA : SomeWinFormsControl, IHaveProperties { ... }
public class MyControlB : SomeOtherWinFormsControl, IHaveProperties { ... }
public class MyProperties
{
public int Foo { get; set; }
public string Bar { get; set; }
public double Baz { get; set; }
...
}
This allows us to add the same additional properties to many different controls, a base class that we cannot change, as well as save / load property sets.
Now that we have about a dozen different MyControlX, we realized that it would be nice to be able to bind data, say, to Properties.Bar.
Obviously, we could do this as follows:
public interface IHaveProperties
{
public MyProperties Properties { get; set; }
public string Bar { get; set; }
}
public class MyControlA : SomeWinFormsControl, IHaveProperties
{
public string Bar
{
get { return Properties.Bar; }
set { Properties.Bar = value; }
}
}
... but we would have to put the same code in each of a dozen controls that seems smelly.
We tried this:
myTextBox.DataBindings.Add(new Binding("Text", myDataSet, "myDataTable.someColumn"))
myTextBox.DataBindings.Add(new Binding("Properties.Bar", someObject, "AProperty"))
myControl.Properties.Bar MyProperties , ?