0

I have populated a listbox with checkboxes content using this code:

<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="12,105,6,100">
            <ListBox Name="ContactResultsData" ItemsSource="{Binding}" Height="393" Margin="0,0,0,0" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox  Name="xxx"  Content="{Binding Path=DisplayName, Mode=TwoWay}" Unchecked="xxx_Unchecked" Checked="xxx_Checked"></CheckBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
</StackPanel>

Now I want to check all checkboxes on a button click, please help.

XIMRX
  • 2,130
  • 3
  • 29
  • 60

1 Answers1

4

There are 2 options available to you.

  1. Add another (Boolean) property to the object in the collection you are binding to the ItemsSource of the ListBox. You would then bind this to the IsChecked property of the checkbox. Once done, in response to your button click you would just need to iterate over the collection and set all the properties to true, then as long as you are notifying of property changed on the bool the UI will be updated.

  2. You could walk the visual tree of the ListBox looking for checkboxes and checking all that you find.

My descriptions may make option 1 seem like more work but that's what I'd recommend.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143