Linking flags - which is better?

I have over 20 checkboxes in my wpf form. I need to store IsChecked values ​​from all of them in some object.

I know two ways.

1) bind all flags to the corresponding properties of the object using the dependency property, such as here

2) handle the Clicked event of all of them

Which solution is better? Is there a better solution that takes up less space in the code?

+6
source share
4 answers

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; } } 
+3
source

Binding

The reason is simple. If you decide to connect to the IsChecked event, you will have to have additional code to figure out which property this checkbox has.

Worse, you have a method for everyone.

With binding, when you check the box, you're done. All you have to do is save the object at the end.

+2
source

If you use a good MVVM structure, you can use the binding without doing it manually (just name them for some kind of convention). I like Caliburn Micro but there are many good ones.

+2
source

To preserve the state of IsChecked, I suggest following the first one ( using binding ), this is better because binding allows you to keep the UI and code clean and decoupled. The second ( handling an event ) is more like WinForms approach, so I don’t understand why you should follow it in a WPF application.

EDIT: answer a question about multiple properties

If it depends on what is actually associated with the view and how the flags are placed in the view. If you use an ItemsControl container, such as ListView , and each flag belongs to one row / column, you can bind all the flags to one collection, for example

 private IList<bool> states; public IList<bool> States { get { return this.states; } set { this.states = value; this.OnPropertyChanged("States"); } } 

To give you a specific answer - please share the layout of the user interface of the form where the flags are located.

+1
source

Source: https://habr.com/ru/post/897836/


All Articles