I know this is probably really daft, but my head isn’t working properly today.
I have a patient entity and a disability table. A patient can have 0 or many disabilities.
If I create a ListBox whose ItemsSource is bound to an ObservableCollection of Disability how do I hook the checked/unchecked of the items CheckBox to add/remove the disability from my patients’ collection of Disabilities using MVVM? What would the CheckBox be bound to on my patient entity?
The
CheckedListBoxexpects to be bound to a list of items that have a boolean property which indicates the state of each checkbox. However, your model has a collection property that you wish to add / remove items to / from based on the checkbox state.This sounds like the job of a view model!
From your
PatientViewModelexpose a collection ofDisabilityViewModelinstances that are bound to yourCheckedListBox, eachDisabilityViewModelinstance should have aIsCheckedboolean property. Detect the changing of this state within your view model, then withinPatientViewModelupdated the state of the wrappedPatientmodel object.