6

I have more than 20 checkboxes in my wpf form. I need store IsChecked values from all of them in some object.

I know two ways.

1) bind all checkboxes to corresponding properties in object using dependency property like here

2) handle Clicked event of all of them

Which solution is better? Is there a better solution taking up less space in code-behind?

Community
  • 1
  • 1
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93

4 Answers4

3

Definitely use a Binding

If your CheckBoxes are unrelated and are all over the place, you'll need 20 different dependency properties to bind to in your DataContext or ViewModel

If your CheckBoxes are all together, such as listed one after another or in a Grid, you can put them in a collection and bind an ItemsControl to them

<ItemsControl ItemsSource="{Binding Options}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Description}" 
                      IsChecked="{Binding IsChecked}" />
        </DataTemplate>
    </ItemsControl>
</ItemsControl>

Your ViewModel or DataContext would contain something like this:

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 would simply be

public class Option
{
    public string Description { get; set; }
    public bool IsChecked { get; set; }
}
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • +1 - although I prefer using IsSelected; IsChecked implies that the object will always be bound to a checkbox (or similar). – Metro Smurf Sep 22 '11 at 17:05
2

Binding.

The reason is simple. If you decided to hook up to the IsChecked event you would have to have extra code to figure out which property to which check box.

Worse case is you have a method for each.

With binding once you set it to the checkbox you are done. All you have to do is save the object at the end.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • Still, there will be about 20 dependency properties in code-behind, right? One property per checkbox. – Ondrej Janacek Sep 22 '11 at 15:22
  • @Andrew Yes, although if your CheckBoxes are all in the same area you could do a List of objects that contain `Description` and `IsChecked`, and bind them to an `ItemsControl`. – Rachel Sep 22 '11 at 15:37
  • But you don't really need to deal with dependency properties. You could have a case where you use the same click event for all 20 and then update an object array but you would still still require logic to determine which check box called the event. But if you have an object array you would probably be using something like a ListBox for the objects already. – paparazzo Sep 22 '11 at 15:41
  • @Andrew It's too long to put in a comment so I put it in it's own answer – Rachel Sep 22 '11 at 16:01
2

If you use a good MVVM framework you can use binding without having to do them by hand(only name them to some convention) - I like Caliburn Micro but there are a lot of good ones out there.

Random Dev
  • 51,810
  • 9
  • 92
  • 119
1

To store IsChecked state I would suggest to follow the first one (using binding), it is better because binding allows keep UI and code behind more clean and decoupled. Second one (handling an event) is most like WinForms approach so I do not see why you should follow it in WPF Application.

EDIT: Answer to question regarding multiple properties

If depends on what actually is bound to a View and how check boxes are placed in the View. If you're using some ItemsControl container like ListView and each check box belong to single row/column - you can bind all checkboxes to a single collection like

private IList<bool> states;
public IList<bool> States
{
   get 
   {
      return this.states;
   }

   set
   {
      this.states = value;
      this.OnPropertyChanged("States");
   }
}

To give you a concrete answer - share please UI layout of Form where checkboxes are placed.

sll
  • 61,540
  • 22
  • 104
  • 156
  • Yay, try to explain it better please, my english is not good enough. Which number is better? – Ondrej Janacek Sep 22 '11 at 15:13
  • @Andrew : see updated answer, basically first one - 1, second - 2 :). From my point of View binding much better just to store values – sll Sep 22 '11 at 15:16
  • It is Grid with 2 columns and 20 rows. There is about 25 checkboxes sitting one per cell in both columns. There are also some another components above, among and below checkboxes. Can you imagine? – Ondrej Janacek Sep 22 '11 at 15:39