Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

SQL
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
Posted
Updated 6-Apr-12 12:25pm
v2

1 solution

Hello

Use ObservableCollection<T> instead of List<T>
It's possible in Windows application, you should add WindowsBase to the references to access.
For more information about this collection look at this: ObservableCollection<t> Class

Then you must implement the INotifyPropertyChanged interface.
Look at this: INotifyPropertyChanged Interface

Then you can define bindings to do some events. For example enabling the controls.

ObservableCollection<t> in Winforms and possible alternatives
 
Share this answer
 
v4
Comments
Mike Poz 7-Apr-12 0:54am    
Thanks, I'll look into that. From the light reading I've done, it looks like that's a WPF collection object, so it may be something I can use to make my WPF version of the sample client less complex as well.
Shahin Khorshidnia 7-Apr-12 4:48am    
ObservableCollection and INotifyPropertyChanged are also used in MVVM for WPF but it doesn't mean that they are not available for Windows forms.
Mike Poz 7-Apr-12 11:33am    
We are using INotifyPropertyChanged already (that was one of the first things I made sure the developers put into the data object classes), and as I said, I have it all working in my WPF sample client (albeit without ObservableCollection), I was working to get it functional in my C# Winforms sample client. To that end I have *a* solution, but not the optimal solution and I will look into ObservableCollection as a possibility for that in both the WPF sample client and the C# Winforms sample client. Again, thanks for your feedback!
Shahin Khorshidnia 7-Apr-12 12:50pm    
Look at this MSDN has a sample code taht INotifyPropertyChanged works in WinForm.

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900