views:

154

answers:

5

Hi everyone,

I have nested groupboxes, which logically represent nested data structures in my application. Let's say i have a structure like this:

Dev1  
- CDev1  
- CDev2  
    - ICDev1  
    - ICDev2

I have checkboxes to enable/disable each of these Devs. I want to bind the CheckState of the child checkboxes to the parent checkbox's CheckState. I want the mechanism to work like this: When i check CDev2, ICDev1 & ICDev2 get automatically checked. But when I uncheck ICDev1, CDev2 stays in its own state. Basically, i want these events to be propagated to children but not to parent, like one way binding.

I am using .Net 2.0 SP2. I don't know if this is possible or not, therefore i would be glad if you show me some pointers about this. If it's not possible, i am going to implement event handlers for all checkboxes.

A: 

This is theoretically possible using two-way binding in WPF (here's a tutorial on data binding in WPF). I don't think you can do it automatically using WinForms. If you're using WinForms, you'll have to catch and handle events to manually modify state changes of that sort.

Randolpho
+4  A: 

Try this:

childCheckBox.DataBindings.Add("Checked", parentCheckBox, "Checked");

You might also want to take a look at the TreeView control, it has the option to display CheckBox next to each item.

Julien Poulin
A: 

Binding won't solve your problem. If you find a way to bind ICDev1 and ICDev2 to CDev2, that will mean that when ICDev1 is checked, both ICDev1 and ICDev2 get checked and vice versa. If that is what you want, you don't need checkboxes for ICDev1, ICDev2, only for CDev2. If you want user to be check ICDev1, but uncheck ICDev2, you need to implement event handlers.

Juozas Kontvainis
A: 

It is possible in WPF, but I don't think it is possible in winforms because there is no property update event mechanism like dependency property or INotifyPropertyChanged in WinForms

ArsenMkrt
A: 

Take a look here, and the link to the Truss library by Kent Boogart, which circumvents winforms altogether. It may require some subclassing though, depending on your model, since it works similar to wpf binding.

kek444