mardi 4 juillet 2017

How to handle many checkboxes in MVVM (WPF)?

I have a list of extensions each with it's own checkbox that user needs to choose from: enter image description here

In the end I need to have a List or an ObservableCollection of all the extensions chosen.

Doing this non-MVVM is quite straight forward. I create the event of Checked or Unchecked, and I add the content of the sender.

View:

<CheckBox Checked="ToggleButton_OnChecked" Unchecked="ToggleButton_OnUnchecked">exe</CheckBox>

Code Behind:

    private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
    {
        var ext = ((CheckBox) sender).Content.ToString();
        Model.FirstRun.ExcludeExt.Add(ext);
    }

    private void ToggleButton_OnUnchecked(object sender, RoutedEventArgs e)
    {
        var ext = ((CheckBox)sender).Content.ToString();
        Model.FirstRun.ExcludeExt.Remove(ext);
    }

If I want to do this MVVM style, as far as I know - I have to create a property for each one of the checkboxes and bind them to the UI. This is a lot of work.

Is there any simpler way?




Aucun commentaire:

Enregistrer un commentaire