Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkboxes binding - what is better solution?

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?

like image 818
Ondrej Janacek Avatar asked Sep 22 '11 15:09

Ondrej Janacek


People also ask

How to bind a checkbox to a V-model in HTML?

Input Binding with Checkbox Option use checked property and change event for the v-model directive. We can use a single option or multiple options for the checkbox element. <input v-model="checked" type="checkbox" name="check" /> <label for="check"> { {checked}}</label>

What are the benefits of using checkboxes instead of items?

You can give users a bigger hit target and clearer interaction cue by converting your checkboxes into check tokens. 4: Faster to scan than a laundry list of items. Contrary to popular belief, a vertical list of items is slower to scan than a horizontal list because it requires more saccades ( research study ).

How do I bind a checkbox to the dependency property?

Show activity on this post. Show activity on this post. Once that is done, you bind the checkbox to the dependency property: For that to work you have to name your Window or UserControl in its openning tag, and use that name in the ElementName parameter. With this code, whenever you change the property on the code side, you will change the textbox.

Is the label bound to the checkbox?

Both the label and the checkbox are bound to it. If the checkbox changes, the label changes too. Show activity on this post. Hello this is my first time posting so please be patient: my answer was to create a simple property: Show activity on this post.


3 Answers

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; }
}
like image 55
Rachel Avatar answered Oct 03 '22 06:10

Rachel


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.

like image 42
David Basarab Avatar answered Oct 03 '22 05:10

David Basarab


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.

like image 28
Random Dev Avatar answered Oct 03 '22 04:10

Random Dev