Reputation: 3431
I have one listBox with check box, when an event end, I need uncheck each check box.
This is my xaml, but in code how can I clear this 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."); }
}
Upvotes: 4
Views: 855
Reputation: 84657
You are binding ItemsSource
in the ListBox
and the ListBox
is using VirtualizingStackPanel
as its ItemsPanel so all ListBoxItem
containers may or may not be generated at any given time.
So even if you search the Visual Tree etc. to get all the CheckBoxes to uncheck, you can't be sure that you've unchecked them all.
I suggest you bind IsChecked
to a new property in the same class as you've defined OperatorNum
(which, by the looks of your question, probably is Employee or similar). This way, all you need to do to uncheck the CheckBoxes is to set IsChecked
to False
in your source class.
Also make sure you implement INotifyPropertyChanged
Example Xaml
<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));
}
}
}
Upvotes: 2
Reputation: 30097
This post explains how you can get the items of a Template
based ListBox
. You can use similar technique to find the CheckBox
and once you have have got the CheckBox
, checking or unchecking is simple
Upvotes: 1