binding CheckBox.Enabled to ComboBox selection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a combo which is bound to a List<MyCustomObject>.
One of the properties of MyCustomObject is a boolean which I would like to
bind to a checkbox so that depending on the selection on the combobox, the
checkbox below it would get enabled/disabled accordingly.

How can this be achieved through databinding? I don't want to implement
this through any event handling - just pure databinding.

My initial effort was this:
checkbox.DataBindings.Add("Enabled", combobox,
"SelectedItem.MyBooleanProperty");

However, the above code does not work.
Thanks
 
Hi, Jiho:

In theory, to bind an object member to another, the first must implement
IListSource interface.

If you want to bind the contents of your generic List<MyCustomObject>, you
should ensure that it implements that, so maybe it would be better to use
another kind of collection instead List<>. Try to use, for example, a custom
collection that implements IList<> as well as IListSource.

Kind regards
 
Thanks for your response. But I am still a bit confused.

I have other controls such as checkboxes and texboxes that are bound to a
custom object, a plain object that doesn't implement any interfaces
whatsoever.

And I am able to bind using, for example:

OnCheckbox.DataBindings.Add("Checked", myCustomObject, "On");

So in essence, my original problem is same except that at the time of this
binding setup, the object is not available because that object would be
selected by the combo. The combo's SelectedItem property would return
another plain object and I am trying to bind to one of its properties to this
checkbox. It's basically cascade (or nested) binding, like master-detail
(which I searched for and all I got was using two datagrids)
 
Hi again, Jiho:

I've been trying to solve the problem and I think I've found a solution. If
you bind the control (combo) to the other (checkbox) It doesn't work because,
as it seems, combobox doesn't implement IListSource.

Anyway, try to bind the CheckBox to the List<MyCustomObject> object. I did
it and it worked.

Here is the code portion I wrote to prove it:

List<MyCustomObject> collection = new List<MyCustomObject>();
collection.Add(new MyCustomObject());
MyCustomObject obj2 = new MyCustomObject();
obj2.BooleanProperty = true;
comboBox1.DataSource = collection;
collection.Add(obj2);
comboBox1.DisplayMember = "BooleanProperty";

Binding binding = new Binding("Checked", collection,
"BooleanProperty");
this.checkBox1.DataBindings.Add(binding);

Assuming that BooleanProperty is false by default at the above code.

I hope this time you got the correct answer.

Regards.
 
Back
Top