5

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?

Oblivious Sage
  • 3,326
  • 6
  • 37
  • 58
  • Aa a minimum I think you need to implement INotifyPropertyChanged on both sides. – Maarten Jun 17 '14 at 15:15
  • Related: http://stackoverflow.com/questions/8894103/does-data-binding-support-nested-properties-in-windows-forms – Maarten Jun 17 '14 at 15:18
  • 1
    @Maarten Yep. That question covers property nesting on the datasource side of the binding, while mine is about property nesting on the control side. – Oblivious Sage Jun 17 '14 at 16:56

1 Answers1

-2

Shouldn't it be this:

TextBox.DataBindings.Add(new Binding("Text", someObject, "Properties.Bar"));
Volma
  • 1,305
  • 9
  • 17