List of Checkboxes

Topics: Questions
Jul 9, 2012 at 5:27 PM

This is probably more of an MVVM question than a Catel question, but the correct solution is clear to me and I'm hoping for an arrow in the right direction.  What is the best way to display a full set of options as checkboxes, where only the checked items are added to a model property?

Let's say we have Sundae models that contain a collection of Topping models, of which there is a master list (Peanuts, Caramel, etc.).  I want to display the full list as checkboxes in an ItemsControl and allow the user to make selections.

If my SundaeModel exposes the master list of Toppings, Catel will generate the corresponding ToppingModel for each one.  However, setting the IsSelected property on ToppingModel based on the set of SelectedToppings is a challenge because SundaeModel doesn't have references to the viewmodels.  I can get this to work using InterestedIn but that creates more problems when multiple SundaeViews are on screen at once.

Is this a situation where SundaeModel should directly create a collection of ToppingModels rather than let Catel do it?

Coordinator
Jul 9, 2012 at 6:55 PM

Such situations are always complex. What I normally do is this:

1) At initialization, create a list of SelectableTopping models (bool IsSelected, string Name)

2) Set the right IsSelected items from the model

3) let the user select/deselect the stuff

4) In the Save method of the view model, check what SelectableTopping models are selected and calculate your own delta. Add/remove as you wish.

 

This has always worked out for me without any issues.

Jul 9, 2012 at 7:12 PM

Okay, that's what I did.  Manually creating models just didn't feel like the One True Way.  :)