我有一个带复选框的列表框,当事件结束时,我需要取消选中每个复选框。
这是我的 xaml,但在代码中如何清除此值?谢谢
<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."); }
}
最佳答案
您正在 ListBox
中绑定(bind) ItemsSource
并且 ListBox
使用 VirtualizingStackPanel
作为它的 ItemsPanel 所以所有 ListBoxItem
容器可能会或可能不会在任何给定时间生成。
因此,即使您搜索 Visual Tree 等以取消选中所有 CheckBox,您也无法确定是否已取消选中它们。
我建议您将 IsChecked
绑定(bind)到您定义的同一类中的新属性 OperatorNum
(从您的问题来看,它可能是 Employee 或相似的)。这样,取消选中复选框所需要做的就是在源类中将 IsChecked
设置为 False
。
还要确保你实现了 INotifyPropertyChanged
Xaml 示例
<CheckBox Grid.Column="0"
Name="CheckBoxZone"
Content="{Binding OperatorNum}"
Tag="{Binding TheValue}"
IsChecked="{Binding IsChecked}"/>
员工
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));
}
}
}
关于c# - 清除 CheckBoxList 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7030804/