[ACCEPTED]-What's the simplest way to bind a list of checkboxes to a list of checked values-binding

Accepted answer
Score: 13

I've found a solution to my problem.

I've 15 changed my ItemsControl to a ListBox, and added a binding between 14 the SelectedItems with my ItemsToGenerate collection using 13 the technique described here. It basically allows 12 me to synchronize any custom collection 11 to ListBox.SelectedItems using a simple attached property.

<ListBox ItemsSource="{Binding AvailableItems}"
         Behaviors:MultiSelectorBehaviours.SynchronizedSelectedItems=
             "{Binding ItemsToGenerate}"
         SelectionMode="Multiple"
         Background="{x:Null}">  
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />                    
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding}"
                      Margin="3"
                      IsChecked="{Binding RelativeSource=
                           {RelativeSource Mode=FindAncestor,
                            AncestorType={x:Type ListBoxItem}},
                           Path=IsSelected}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>

I'm 10 still able to display this as I initially 9 wanted (a list of checkboxes), by adding 8 a data template to change each ListBoxItem to a checkbox 7 and binding each Checkbox.IsChecked to ListBoxItem.IsSelected.

I had this pattern 6 in so many places in my application that 5 this is the ideal solution for me, because 4 now I just need to specify one attached 3 property, and the rest is all handled by 2 the data bindings, and I don't need any 1 additional code.

Score: 10

I honestly would create a list of objects 21 containing both the string and a boolean 20 indicating if it is checked.

With a little 19 Linq you can generate your list of objects 18 and bind it to itemSource instead of binding 17 the list of strings.

It will be simpler 16 in the end, especially if you actually need 15 to update something if the user is allowed 14 to check/uncheck the checkboxes.

== update 13 ==

in answer to the comment, my take on this 12 because I'm not sure I understand what the 11 actual problem would be: provided we have 10 the full list (AvailableItems) and the list 9 of selected items (ItemsToGenerate):

public class ItemEntry
{
  public string Name { get; set; }
  public bool IsSelected {get; set; }
}

...

_Items = from item in AvailableItems
            select new ItemEntry() { 
                    Name = item, 
                    IsSelected = ItemsToGenerate.contains(item)
                  }).ToList();

You 8 can then bind your list like so, by exposing 7 _Items as a property named Items:

<ItemsControl ItemsSource="{Binding Path=Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>    
</ItemsControl>

You can 6 at a later time select from _Items where 5 IsSelected is true to get the selected items 4 if you need to.

Also, if ItemsToGenerate 3 can get big, you should create a HashSet 2 of the values and use it in the query, that 1 should make it faster if need be.

More Related questions