views:

547

answers:

1

I have an entities model with a many to many relationship. For simiplicity, lets assume its a car entity and a feature(cd player, moonroof, etc.) entity.

I have a Silverlight/WPF form in which you edit the car entity. I would like to have the list of possible features (everything in the features table) be a list of checkboxes. That part is easy. However, I am trying to think of an elegant way to bind the checkboxes so that when I check, uncheck one of them, it add/removes the association between that feature and car.

I know that this can easily be done with good old WinForms-style event handlers, but I would like to know if anybody has a way of doing this using data binding so that I can still keep my presentation and my logic separated

+2  A: 

Have you looked at the MVVM presentation pattern?

I would suggest creating a ViewModel class to bind to. This class could contain properties like HasSunroof for the checkboxes to bind to. In the set methods of these properties they could alter the related property (like the Sunroof property).

An alternative would be to use an IValueConverter. Bind to the actual property (say Sunroof). In the converter, return true (checked) if the value of Sunroof is not equal to null, otherwise return false. The ConvertBack() function could turn the case where IsChecked is equal to true into the required object for the property.

Josh G