Hi all,
I'm working on a sample client for my company and I'm trying to use databinding for enabling/disabling controls in the Winform UI.
I have a populated object structure that I parse through and dynamically create controls (Labels, Radiobuttons, Checkboxes, Textboxes, etc.) and insert them onto the form.
At the time of creation, I am able to set databinding so that the control honors the object's "IsAvailable" property and use that to control the "Enabled" property on the control, so that when the form draws initially, the controls that need to be disabled are not available while the controls that need to be enabled are available.
I have also set a databinding to update the source when a control is changed (Box checked, button selected, etc.) and all of this works perfectly.
The problem is that some of the disabled controls need to be enabled in the UI when some other control is selected.
Example: Radiobutton A, when selected, should enable Checkbox1 through Checkbox10.
The code behind is properly setting the property in the object that says the checkboxes should be available, and the events are happening that inform the client that these changes happened, but I'm unable to use databinding to force the controls with updated properties to actually reflect that enabled state change in the Windform UI.
I tried this:
textbox.DataBindings.Add(new Binding("Enabled", answer, "IsAvailable", true));
textbox.DataBindings[0].ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
However it does not invoke a UI update on the fly. I also tried using the change events to step through every control in the UI and refresh each and it still doesn't honor any changes in the datasource that would alter the control's enabled state.
There is no method that I can find, method to "rebind" a control to a datasource to force the UI update and so I was wondering if, perhaps, someone knows of a way to make this work that is as simple as WPF (where I have it working smoothly).
Any pointers or suggestions would be greatly appreciated.
{Update}
I should tell you that my current solution is to use Control.Tag to store a reference to the data object the control represents, and when I get the notification of a change, I walk through the UI controls and then pull the data object reference out and use that to set the control's availability.
Like this:
((CheckBox)control).Enabled = ((Answer)((CheckBox)control).Tag).IsAvailable;
However this, to me, is a hack that databinding should be able to resolve, so if there are any other solutions out there, I would be happy to hear of them.
Thanks again!
Thanks!
Mike Poz