Definitely use binding
If your CheckBoxes are unrelated and are everywhere, you will need 20 different dependency properties to bind in your DataContext or ViewModel
If your CheckBoxes are together, for example, listed one by one or in the Grid, you can put them in a collection and bind ItemsControl
to them
<ItemsControl ItemsSource="{Binding Options}"> <ItemsControl.ItemTemplate> <DataTemplate> <CheckBox Content="{Binding Description}" IsChecked="{Binding IsChecked}" /> </DataTemplate> </ItemsControl> </ItemsControl>
Your ViewModel or DataContext will contain something like the following:
private List<Option> options; private List<Option> Options { get { if (options== null) { options = new List<Option>(); // Load Options - For example: options.Add(new Option { Description = "Option A", IsChecked = false }); options.Add(new Option { Description = "Option B" }); options.Add(new Option { Description = "Option C", IsChecked = true}); } return options; } }
And your Option
class will just be
public class Option { public string Description { get; set; } public bool IsChecked { get; set; } }
source share