Clear CheckBoxList values?

I have one listBox flag, when the event ends, I need to uncheck it.

This is my xaml, but in code, how can I clear these values? Thanks

<ListBox Name="_employeeListBox" ItemsSource="{Binding employeeList}" Margin="409,27,41,301"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <CheckBox Grid.Column="0" Name="CheckBoxZone" Content="{Binding OperatorNum}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked"/> <TextBlock Grid.Column="1"></TextBlock> <TextBlock Grid.Column="2" Text="{Binding Name}"></TextBlock> <TextBlock Grid.Column="3"></TextBlock> <TextBlock Grid.Column="4" Text="{Binding LastName}"></TextBlock> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> private void _searchProjectsbyEmployeebutton_Click_1(object sender, RoutedEventArgs e) { List<EmployeebyProject> employeebyProjectList = new List<EmployeebyProject>(); if (EmployeeCollectionToInsert.Count != 0) { foreach (var employee in EmployeeCollectionToInsert) { foreach(var employeebyProject in employee.EmployeebyProject) { employeebyProjectList.Add(employeebyProject); } } LoadEmployeebyProject(employeebyProjectList); //HERE I NEED UNCHECKED the ListBoxChecked. } else { MessageBox.Show("Please select an employee to search his project."); } } 
+4
source share
2 answers

You bind the ItemsSource to a ListBox , and the ListBox uses VirtualizingStackPanel as its ItemsPanel, so all ListBoxItem containers may or may not be generated at any given time.
Therefore, even if you are viewing Visual Tree, etc., to clear all CheckBox checkboxes, you cannot be sure that you have unchecked them all.

I suggest you bind IsChecked to a new property in the same class that you defined OperatorNum (which, by the look of your question, is probably Employee or similar). So all you have to do to uncheck the checkbox is to set IsChecked to False in the source class.
Also make sure you implement INotifyPropertyChanged

Xaml example

 <CheckBox Grid.Column="0" Name="CheckBoxZone" Content="{Binding OperatorNum}" Tag="{Binding TheValue}" IsChecked="{Binding IsChecked}"/> 

Employee

 public class Employee : INotifyPropertyChanged { private bool m_isChecked; public bool IsChecked { get { return m_isChecked; } set { m_isChecked = value; OnPropertyChanged("IsChecked"); } } // Etc.. public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } 
+2
source

This tells you how you can get ListBox based Template elements. You can use a similar technique to find a CheckBox , and once you have a CheckBox , checking or unchecking is simple

+1
source

All Articles