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 a lot of different controls whose base class we can't modify, as well as save/load sets of properties.
Now that we have about a dozen different MyControlX, we've realized it would be nice to be able to data bind to, say, Properties.Bar
.
Obviously we could do it like this:
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 that same code in each of the dozen or so controls, which seems kind of smelly.
We tried this:
// Example: bind control's property to nested datasource property
myTextBox.DataBindings.Add(new Binding("Text", myDataSet, "myDataTable.someColumn"))
// works!
// bind control's (nested) Properties.Bar to datasource
myTextBox.DataBindings.Add(new Binding("Properties.Bar", someObject, "AProperty"))
// throws ArgumentException!
Is there some way to bind to myControl.Properties.Bar
by constructing the binding a certain way or modifying the MyProperties
class, rather than making identical changes to all the controls?