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?
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>
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 ).
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.
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.
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; }
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With