What's the simplest way to bind a list of checkboxes to a list of checked values

后端 未结 2 606
说谎
说谎 2021-02-14 06:38

I have a list of AvailableItems that I want to display as a list of checkboxes, so that users can pick which items to generate, which are then stored in another lis

2条回答
  •  春和景丽
    2021-02-14 07:33

    I've found a solution to my problem.

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

      
        
            
                                    
            
        
        
            
                
            
        
    
    

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

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

提交回复
热议问题