0

I have a data entry form, with its DataCountext bound to a ViewModel object.

The form contains (among other things) a number of CheckBox elements, each bound to a boolean in the ViewModel. These CheckBoxes are grouped into two GroupBoxes.

There are two business requirements I don't know how to handle.

  1. At least one checkbox in groupbox 1 is selected, or at least one checkbox in groupbox 2 is selected.
  2. If anything in groupbox 1 is selected, everything in groupbox 2 is unselected, and vice versa.

Anyone have any ideas?

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165

2 Answers2

0

Sounds like something I'd cover in the ViewModel. The VM get's an inital state, which is already a valid state (something is selected). If the user now selects something else, you can cover that by setting the properties of the other group to false.

Martin Moser
  • 6,219
  • 1
  • 27
  • 41
  • It's easy enough to put a method in the ViewModel that checks to see if the rules are violated. But I'm not sure how to tie that into the form validation behaviors displayed in the View. What I'd want, if no checkboxes are checked, is to see the GroupBox surrounded with the red border and an appropriate message displayed in the Validation.Errors. In my playing around with IDataErrorInfo, I've been able to get the red box around the individual controls, but I've no idea how to get it to draw around the GroupBox. – Jeff Dege Apr 16 '12 at 15:13
0

Solution for problem 1 is simple - have the Property setter in the VM clear the fields that should not be set. Binding will update the display appropriately.

Solution for problem 2 is a bit more complicated. If I want to display an error at the GroupBox level, I need to attach a BindingGroup to the GroupBox, and add an appropriate ValidationRule to it.

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165