清除 CheckBoxList 的值?
我有一个带有复选框的列表框,当事件结束时,我需要取消选中每个复选框。
这是我的 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."); }
}
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."); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在
ListBox
中绑定ItemsSource
,并且ListBox
使用VirtualizingStackPanel
作为其 ItemsPanel,因此所有ListBoxItem
容器可能会也可能不会在任何给定时间生成。因此,即使您搜索视觉树等以取消选中所有复选框,您也无法确定是否已全部取消选中。
我建议您将
IsChecked
绑定到与您定义的OperatorNum
相同的类中的新属性(从您的问题来看,它可能是 Employee 或类似的属性)。这样,您只需在源类中将IsChecked
设置为False
即可取消选中复选框。另请确保您实现了
INotifyPropertyChanged
Example Xaml
Employee
You are binding
ItemsSource
in theListBox
and theListBox
is usingVirtualizingStackPanel
as its ItemsPanel so allListBoxItem
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 definedOperatorNum
(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 setIsChecked
toFalse
in your source class.Also make sure you implement
INotifyPropertyChanged
Example Xaml
Employee
这篇文章介绍了如何获取
模板的项目
基于ListBox
。您可以使用类似的技术来查找CheckBox
,一旦获得CheckBox
,选中或取消选中就很简单This post explains how you can get the items of a
Template
basedListBox
. You can use similar technique to find theCheckBox
and once you have have got theCheckBox
, checking or unchecking is simple