7

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?

5
  • don't you think the UI little weird with lots of checkboxes? how about having combo box with check box in it? Commented Jul 4, 2017 at 8:43
  • @SushilMate would that make it easier to implement the binding in MVVM ? Commented Jul 4, 2017 at 8:46
  • btw this is just the barebone UI, a designer will later improve it. Commented Jul 4, 2017 at 8:47
  • You should first create a model. I.e class myCheckBox { boo Active; string Name; } class myCheckBoxList { List<myCheckBox >() ; ... Add(myCheckBox paramMyCheckBox ) { myCheckBox .Add (paramMyCheckBox ); } } After Render the model on view *Sorry but I do not have a compiler here to write correct code Commented Jul 4, 2017 at 8:48
  • @DavidRefaeli Yes. code-wise, design-wise, effort-wise. Commented Jul 4, 2017 at 8:54

1 Answer 1

9

You should think about a structure first and then it is not that much work and pretty straight forward to use.

For example: you have groups of extensions. I would create somethink like this (just pseudo code; e.g. no INotifyPropertyChanged implemented).

public class ExtensionGroup
{
    public string Name {get; set;}
    public ObservableCollection<ExtensionInfo> ExtensionInfos {get; set;}
}

public class ExtensionInfo
{
    public string Extension {get; set;}
    public bool IsChecked {get; set;}

    public ExtensionInfo(string extension)
    {
        Extension = extension;
    }
}

And in your ViewModel you can create

public ObservableCollection<ExtensionGroup> ExtensionGroups {get; set;}

Now you have to add the data like this

var extensionGroup = new ExtensionGroup{Name = "Executables"};
extensionGroup.ExtensionInfos.Add(new ExtensionInfo("exe");
extensionGroup.ExtensionInfos.Add(new ExtensionInfo("bat");

and after that you have a list with all groups including the extensions.

In you view you can bind this data and you're done. For the view you could use a ListView with a DataTemplate or some other items control.

Your view will get a lot easier with less code because you just have to specify the layout one time.

/edit This is a sample implementation of the xaml. I used a ItemsControl instead of a ListView.

<ItemsControl ItemsSource="{Binding ExtensionGroups}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Label Content="{Binding Name}"/>
                <ItemsControl ItemsSource="{Binding ExtensionInfos}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsChecked}"
                                      Content="{Binding Extension}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Looks like this (just added two groups with two extensions). Example

Sign up to request clarification or add additional context in comments.

3 Comments

And then how would you do the binding?
Amazing - I'm going to try it out :-)
Works like a charm. You just forgot (in the answer) to initalize (ExtensionGroups = new ObservableCollection<ExtensionGroup>(); var extensionGroup = new ExtensionGroup { Name = "Executables", ExtensionInfos = new ObservableCollection<ExtensionInfo>()}; ) and to add the extensionGroup to ExtensionGroups ( ExtensionGroups.Add(extensionGroup); )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.